这是我的代码:
public static Connection connectToDataBase() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (final Exception e) {
throw new RuntimeException("Driver failure");
}
connectionProperties = new Properties();
connectionProperties.put("user", user);
connectionProperties.put("password", passwor);
datenbankverbindung = DriverManager.getConnection(link, connectionProperties);
return connection;
}
我正在连接到数据库,工作正常。但我想知道登录数据是否错误或者连接是否有问题。
最佳答案
请阅读this
它说 DriverManager.getConnection 抛出 SQLException。所以你可以捕获这个异常,例如:
try{
...
datenbankverbindung = DriverManager.getConnection(link,connectionProperties);
}catch(SQLException e){
...//do whatever you want
}
希望这有帮助。
关于java - DriverManager.getConnection();如何判断是用户名/密码错误还是驱动程序无法连接数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40925635/