其他分享
首页 > 其他分享> > DataSource连接失败常见异常捕获

DataSource连接失败常见异常捕获

作者:互联网

捕获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)

标签:CJException,捕获,System,getCause,DataSource,println,28000,连接,out
来源: https://blog.csdn.net/qq_37252429/article/details/117002617