【发布时间】:2017-05-22 15:20:54
【问题描述】:
我的代码不起作用 java 和 jsp 请帮助我
public class DbConnect {
Connection conn = null;
ResultSet rst;
java.sql.Statement stmt;
String url="jdbc:mysql://localhost:3306/";
String dbName="teljo";
String driver="com.mysql.jdbc.Driver";
String userName="root";
String password="";
public Connection getConnect(){
try {
Class.forName(driver).newInstance();
System.out.println("driver loaded sucessfully");
conn=DriverManager.getConnection(url + dbName,userName,password);
System.out.println("connect to the data base");
}
catch (ClassNotFoundException ce) {
System.out.println("driver loading failed"+ ce);
}
catch (Exception e) {
System.out.println("error in connection to database");
}
return conn;
}
}
这是我的连接代码数据库类。 mysql连接notconnect是怎么回事?我使用 Eclipse IDE, 输出是这样的
driver loading failedjava.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerException
【问题讨论】:
-
如果您的错误消息确实表明未找到类
com.mysql.jdbc.Driverjava.lang.NullPointerException,那么您的driver变量已在您未显示的代码中的某处更新。 -
无关: 不要将
ResultSet和Statement放在DbConnect类中。它们应该是使用它们的局部变量。这些字段很少见。