捕获SQLException异常,并对常见的异常进行特殊处理:
private static Boolean sqlTest(DataSource dataSource) {
try {
dataSource.setLoginTimeout(5);
dataSource.getConnection().getClientInfo();
}catch (SQLException e) {
if(e.getCause() instanceof CJCommunicationsException){
//S100
CJCommunicationsException cjCommunicationsException = (CJCommunicationsException)e.getCause();
System.out.println("错误码:"+cjCommunicationsException.getSQLState());
System.out.println("错误信息:"+cjCommunicationsException.getMessage());
}else if(e.getCause() instanceof CJException){
//42000,28000
CJException cjException = (CJException)e.getCause();
System.out.println("错误码:"+cjException.getSQLState());
System.out.println("错误信息:"+cjException.getMessage());
}
e.printStackTrace();
return false;
}
return true;
}
S1000 |
jdbc-url错误 |
Could not create connection to database server. Attempted reconnect 3 times. Giving up. |
42000 |
数据库名错误 |
Unknown database 'aaa22' |
28000 |
用户名错误 |
Access denied for user 'root1'@'10.1.23.90' (using password: YES) |
28000 |
密码错误 |
Access denied for user 'root'@'10.1.23.90' (using password: YES) |