【发布时间】:2016-05-27 01:43:56
【问题描述】:
我在我构建的 java 项目中使用静态方法来查询 localhost mysql db。
类似的东西:
public static void sqlQuery() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(localhost,root,password);
stmt = conn.prepareStatement(Some SQL);
rs = stmt.executeQuery();
} catch(Exception e) {
// Error Handling
} finally {
try { if (rs != null) rs.close(); } catch (Exception e) {};
try { if (stmt != null) stmt.close(); } catch (Exception e) {};
try { if (conn != null) conn.close(); } catch (Exception e) {};
}
}
我的问题是我是否可以保留 conn 变量而不关闭它并再次将其重用于另一个查询。
没有其他应用程序使用此数据库,我只需 1 个连接即可完成我想要的操作..
附:对不起我的英语不好..
【问题讨论】:
-
是的,但是这样做会消耗数据库上的资源,并且打开连接的最大数量有限制。