数据库connection为null的处理方法

private String drv = "com.mysql.jdbc.Driver";

private String url = "jdbc:mysql://localhost:3306/demo";

private String usr = "root";

private String pwd = "root";

private Connection conn = null;

private Statement stm = null;

private ResultSet rs = null;

public boolean createConn() {

boolean b = false;
try {
Class.forName(drv).newInstance();
conn = DriverManager.getConnection(url, usr, pwd);
b = true;
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
return b;
}
conn始终为空,检查lib下是否有驱动包,如果问题仍然存在,请尝试更换驱动包。
笔者数据库是mysql5.5
原驱动包是mysql-2.0.4.jar
更新到mysql-connection-java-5.1.18-bin.jar问题解决。

你可能感兴趣的:(com)