jsp与mysql的连接??

AlifeYuan 2005-10-11 11:38:22
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd+";



其中的dbNmae user password指的是哪一个?
dbName应该是你所连接的数据库名称?
那user password指的是哪个用户密码?
谢谢!!
...全文
87 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hy2003fly 2005-10-12
  • 打赏
  • 举报
回复
是啊,rs=stmt.executeQuery(select * from about);里面是String来的.

你改成这样试试.
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con;
Statement stmt;//(或PreparedStatement)
ResultSet rs;
//加载驱动程序
//Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306/first";
String user="root";//(username)
String password="00";
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery("select * from about");
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
con.close();
%>
</body>
</html>

因为你一开始就用了旧的驱动,但是后面又用新的,这样造成了混淆.
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
这一句我一直都没有写的,好像是已经注册了的,所以可以省略.
你写的时候最好用上try...catch语句,追踪一下可能出错的地方,比如说连不数据库又怎样处理.
AlifeYuan 2005-10-12
  • 打赏
  • 举报
回复
难道是(select * from about)改为(("select * from about"))?
又出现错误:
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 5 in the jsp file: /first.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:50: package com.mysql.jdbc does not exist
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
^
1 error


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.

AlifeYuan 2005-10-12
  • 打赏
  • 举报
回复
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con;
Statement stmt;//(或PreparedStatement)
ResultSet rs;
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306(端口)/first";
String user="root";//(username)
String password="00";
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery(select * from about);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
con.close();
%>
</body>
</html>



这样的页面不能连接数据库
不知为何
错误如下:
message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 5 in the jsp file: /first.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:56: ')' expected
rs=stmt.executeQuery(select * from about);
^
1 error


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.

hy2003fly 2005-10-11
  • 打赏
  • 举报
回复
dbName指数据库名,user password是指你登录dbName的用户名和密码.
hy2003fly 2005-10-11
  • 打赏
  • 举报
回复
如果数据库没有设置密码就用你登录mysql的用户名,一般为root.与jsp连接可以这样写
try{
Connection con;
Statement stmt;(或PreparedStatement)
ResultSet rs;
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306(端口)/dbName";
String user="root";(username)
String password=userpassword;
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery(select * from table);
.................
用google搜一下,网上有很多这方面的资料.
mowic 2005-10-11
  • 打赏
  • 举报
回复
String url="jdbc:mysql://localhost/"+dbName+"?user=root&password="";
AlifeYuan 2005-10-11
  • 打赏
  • 举报
回复
建数据库的时候没有设置用户密码,该怎样写此语句??

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧