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="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.*"%>
<%
String dbname = "mydata";
String dbuser = "root";
String dbpwd = "123456";
String dburl = "jdbc:mysql://localhost:3306/"+dbname+"?useUnicode=true&characterEncoding=GB2312";
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection cn = java.sql.DriverManager.getConnection(dburl,dbuser,dbpwd);
Statement stmt = cn.createStatement();
ResultSet rs = stmt.executeQuery("select * from users");
out.print(rs.getString(1));
rs.close();
stmt.close();
cn.close();
%>
关键是网上很多方法都已经试过了,包括:重新引入mysql类库之后重新编译,把jdbc类库加入到环境变量,全部没有用,弄了一整天了,郁闷死,求大神解救!
mysql
数据库
jdbc
jsp
------解决方案--------------------你输出一下SYSO(cn);
然后确定一下是否jar包导错没
------解决方案--------------------Connection conn = java.sql.DriverManager.getConnection(dburl,dbuser,dbpwd);把 java.sql.去掉吧Connection conn = DriverManager.getConnection(dburl,dbuser,dbpwd);这样
------解决方案--------------------你确定你的MYSQL开启了
------解决方案--------------------引入包错误吧,应该都用 java.sql就行了
也就是说 只需要导入包 <%@page import="java.sql.*"%>
------解决方案--------------------把你这些连接代码放在java类中测试一下,能得到具体异常
------解决方案--------------------1、
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
-》
Class.forName("com.mysql.jdbc.Driver").newInstance();
2、
Connection conn = java.sql.DriverManager.getConnection(dburl,dbuser,dbpwd);
-》
Connection conn = DriverManager.getConnection(dburl,dbuser,dbpwd);
------解决方案--------------------String sql= "select * from users";
Statement stmt = cn.createStatement(sql);
ResultSet rs = stmt.executeQuery();
if(rs.next){
rs.getString(1);
}
需要next一下.试试
------解决方案--------------------<%@page import="com.mysql.jdbc.Driver"%>这句删了 很无厘头