MySQL数据库连接问题,郁闷了很久。。。
JSP连接mysql数据库,tomcat报异常,但是代码调试了很久,就算照着书上的源代码敲上去也是报错,以下是报错内容:
org.apache.jasper.JasperException: An exception occurred processing JSP page /WebRoot/index.jsp at line 11
8: String dburl = "jdbc:mysql://localhost:3306/"+dbname+"?useUnicode=true&characterEncoding=GB2312";
9: Class.forName("com.mysql.jdbc.Driver");
10: DriverManager.registerDriver(new com.mysql.jdbc.Driver());
11: Connection cn = java.sql.DriverManager.getConnection(dburl,dbuser,dbpwd);
12: Statement stmt = cn.createStatement();
13: ResultSet rs = stmt.executeQuery("select * from users");
14: out.print(rs.getString(1));
附上代码:
下面 我给你改的,运行在后台有打印地址,说明已经连接成功,你只要确保你的数据库和表的名字没错,一定和数据库连接上了
<%@page language="java" contentType="text/html;charset=utf-8"%>
<%@page import="java.sql.*"%>
<%
String dburl = "jdbc:mysql://localhost:3306/mydata";
String dbuser = "c860";
String dbpwd = "123456";
try{
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection conn = java.sql.DriverManager.getConnection(dburl,dbuser,dbpwd);
System.out.print(conn);//这里打印是否连接上了
Statement stmt = conn.createStatement();
ResultSet rs= stmt.executeQuery("select * from users");
rs.close();
stmt.close();
conn.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>