关闭数据库连接的代码块
代码块语法遵循标准markdown代码,例如:
public static void closePreConn(ResultSet rs, PreparedStatement pstmt, Connection conn) {
DBConfig dbConfig = DBConfig.loadDBConfig();
try {
if (rs != null) {//如果返回的结果集对象不能为空,就关闭连接
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (pstmt != null) {
pstmt.close();//关闭预编译对象
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null) {
if(Common.CONFIG_MONITOR_TYPE_DATABASE.equals(dbConfig.getMonitorType())){
conn.close();//关闭连接对象
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//获取数据库信息的方法
public static DBConfig loadDBConfig(){
if(Util.isNull(DBConfig.dbConfig)){
String rootPath = Util.getRootPath();
File configPathFile = new File(rootPath + "\\configFilePath\\dbConfig.js");
String json = JSON.getStringFromFile(configPathFile);
DBConfig.dbConfig = JSON.fromJson(json, DBConfig.class);
}
return DBConfig.dbConfig;
}
本文详细介绍了如何在Java中关闭数据库连接,包括关闭结果集、预编译对象和连接对象的相关代码实现,确保资源的有效释放。
5133

被折叠的 条评论
为什么被折叠?



