<%@ page contentType="text/html; charset=gb2312" %> <%@ page language="java" %> <%@ page import="com.mysql.jdbc.Driver" %> <%@ page import="java.sql.*" %>
<html>
<body>
<% //驱动程序名 String driverName="com.mysql.jdbc.Driver"; //数据库用户名 String userName="root"; //密码 String userPasswd="123456"; //数据库名 String dbName="student"; //表名 String tableName="studentinfo"; //联结字符串 String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection(url); Statement statement = conn.createStatement(); String sql="SELECT * FROM "+tableName; ResultSet rs = statement.executeQuery(sql); //获得数据结果集合 ResultSetMetaData rmeta = rs.getMetaData(); //确定数据集的列数,亦字段数 int numColumns=rmeta.getColumnCount(); // 输出每一个数据值 out.print("id"); out.print("|"); out.print("num"); out.print("<br>"); while(rs.next()) { out.print(rs.getString(1)+" "); out.print("|"); out.print(rs.getString(2)); out.print("<br>"); } out.print("<br>"); out.print("数据库操作成功,恭喜你"); rs.close(); statement.close(); conn.close(); %>
</body>
</html>我先前已经把mysql-connector-java-5.0.4-bin.jar复制到TOMCAT6.0了,现在上面的代码放到webapps/JDBC下的JSP中
http://localhost:8080/jdbc/a.jsp 还是运行不出啊type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /a.jsp at line 3734:
35: Class.forName("com.mysql.jdbc.Driver").newInstance();
36:
37: Connection conn=DriverManager.getConnection(url);
38:
39: Statement statement = conn.createStatement();
40:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.a_jsp._jspService(a_jsp.java:149)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:812)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3269)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1182)
com.mysql.jdbc.Connection.createNewIO(Connection.java:2670)
com.mysql.jdbc.Connection.<init>(Connection.java:1531)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.a_jsp._jspService(a_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
--------------------------------------------------------------------------------
其次,有servlet exception ,看看该导的包都导了没!
最后是检查你的本地mysql root是否有密码的root用户,可以先自行在console链接测试看看!
grant all on *.* to 'user@host';
或者
jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;你的mysql的端口你更改过了?否则应该是
jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;