如何处理ORA-17820: 网络适配器无法建立连接错误

问题描述 投票:0回答:1

我想在尝试连接时捕获数据库错误

我的代码:

private static String getValueFromDB(String dbConnection,String dbUsername,String dbPassword,String condition,String field){
        String data="";
        try {


            Connection connection = DriverManager.getConnection(dbConnection, dbUsername, dbPassword);
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM NDTBNODE WHERE " + condition);
            preparedStatement.setString(1, licenseKey);
            ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()) {
                // Process the result set
                if(field.equals("DC_ID"))
                    data=String.valueOf(resultSet.getInt(field));
                else
                    data=resultSet.getString(field);
            }
        } catch (java.sql.SQLRecoverableException e) {
        loggerS.error("Recoverable error occurred while connecting to the database: {}", e.getMessage());
    } catch (java.sql.SQLException e) {
        loggerS.error("SQL Exception occurred while interacting with the database: {}", e.getMessage());
    }
        return data;
}

我尝试将 Exception 类放入仍然相同的结果。

我遇到的异常: 2024 年 5 月 11 日上午 10:37:45 oracle.jdbc.driver.PhysicalConnection 连接

我不想修复它,我想处理它

java database spring-boot exception jdbc
1个回答
0
投票

您应该捕获

oracle.jdbc.driver.PhysicalConnection
异常以及您咳嗽的其他异常

} catch (oracle.jdbc.driver.PhysicalConnection ex) {
  // process exception here
}
© www.soinside.com 2019 - 2024. All rights reserved.