我的数据库控制器中有这段代码
package nypHeritage.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBController {
//Declaration of attributes
private Connection con;
/********************************************************
* Method Name : testDriver
* Input Parameter : nil
* Purpose : To test if the driver is properly installed
* Return :nil
*******************************************************/
public void testDriver() throws Exception {
System.out.println("Initializing Server... ");
try {
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println(" Driver Found.");
}
catch (ClassNotFoundException e) {
System.out.println(" Driver Not Found, exiting..");
throw (e);
}
}
public void getConnection(){
String url = "";
try {
url = "jdbc:mysql://localhost/nyp_heritage";
con = DriverManager.getConnection(url, "IT1639User", "user");
System.out.println("Successfully connected to " + url+ ".");
}
catch (java.sql.SQLException e) {
System.out.println("Connection failed ->"+ url);
System.out.println(e);
}
}
/************************************************************
* Method Name : readRequest
* Input Parameter : String (database query)
* Purpose : Obtain the result set from the db query
* Return : resultSet (records from the query)
************************************************************/
public ResultSet readRequest(String dbQuery) {
ResultSet rs = null;
System.out.println("DB Query: " + dbQuery);
try {
// create a statement object
Statement stmt = con.createStatement();
// execute an SQL query and get the result
rs = stmt.executeQuery(dbQuery);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
/***********************************************************
* Method Name : updateRequest
* Input Parameter : String (database query)
* Purpose : Execute update using the db query
* Return : int (count is 1 if successful)
***********************************************************/
public int updateRequest(String dbQuery) {
int count = 0;
System.out.println("DB Query: " + dbQuery);
try {
// create a statement object
Statement stmt = con.createStatement();
// execute an SQL query and get the result
count = stmt.executeUpdate(dbQuery);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/***********************************************************
* Method Name : terminate
* Input Parameter : nil
* Purpose : Close db conection
* Return :nil
**********************************************************/
public void terminate() {
// close connection
try {
con.close();
System.out.println("Connection is closed");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] arg)throws Exception{
DBController db = new DBController();
db.testDriver();
}
}
过去2个月完全正常工作,但现在当我重新打开它时,它向我显示了这个错误
Connection failed ->jdbc:mysql://localhost/nyp_heritage
java.lang.NullPointerException
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/nyp_heritage
DB Query: INSERT INTO PARTICIPANT(name, mobile, monthOfEvent, eventName, dateBirth, address, email, gender) VALUES ('Samantha Jones','+6590287645', 'december', 'Heritage Centres Tour', '19/05/1990', 'Woodlands', 'jones@gmail.com', 'Female')
它说,我的连接失败,并且没有为我的localhost找到合适的驱动程序。帮助PLS!我不知道如何解决这个问题...
答案 0 :(得分:1)
首先,确保您的类路径上有适当的connector mysql。使用JDBC 4.0及更高版本,您不需要
Class.forName("org.gjt.mm.mysql.Driver"); // which should be "com.mysql.jdbc.Driver"
// since the one you appear to be trying
// is truly ancient.
来自DriverManager
javadoc,
应用程序不再需要使用Class.forName()明确加载JDBC驱动程序。
答案 1 :(得分:1)
变化
Class.forName("org.gjt.mm.mysql.Driver");
到
Class.forName("com.mysql.jdbc.Driver");
答案 2 :(得分:0)
根据我的发现
首先,
org.gjt.mm.mysql.Driver is out dated.
所以你应该在Class.forName
部分
com.mysql.jdbc.Driver
资源: What is the jdbc driver "org.gjt.mm.mysql.Driver" for?
第二,
您应该检查项目中是否添加了适当的J连接器jar文件 你可以在这里查看http://dev.mysql.com/downloads/connector/j/
第三, 你可以使用带有资源的try catch块来确保你打开的任何东西都是关闭的,如果它是可以接近的
例如
public void update(String name, String tel, String id) throws SQLException {
System.out.println("in update part tel is " + tel);
String sql = "UPDATE APP.DBNAME "
+ "SET NAME=? , TEL=? WHERE NAME=?";
try (PreparedStatement ps = getConn().prepareCall(sql)) {
ps.setString(1, name);
ps.setString(2, tel);
ps.setString(3, id);
ps.executeUpdate();
}
}
四,
JDBC 4.0功能
感谢Mustang中包含的Java SE Service Provider机制, Java开发人员不再需要使用显式加载JDBC驱动程序 类似于Class.forName()的代码来注册JDBC驱动程序。 DriverManager class通过自动定位合适的驱动程序来解决这个问题 调用DriverManager.getConnection()方法时。此功能 是向后兼容的,因此不需要对现有JDBC进行任何更改 代码。
资源: http://www.onjava.com/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html