我想在尝试连接时捕获数据库错误
我的代码:
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 连接
我不想修复它,我想处理它
您应该捕获
oracle.jdbc.driver.PhysicalConnection
异常以及您咳嗽的其他异常
} catch (oracle.jdbc.driver.PhysicalConnection ex) {
// process exception here
}