根据http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#close()的文档,
当 Statement 对象关闭时,其当前的 ResultSet 对象(如果存在)也将关闭。
但是根据是否必须单独关闭 JDBC 结果集和语句,尽管随后关闭了连接?Connection
Statement
, 明确关闭和似乎是一个好习惯ResultSet
。
如果我们仍然需要 close ResultSet
,我们可能需要一个嵌套的 try-with-resources 语句,因为我们可能会Statement
像这样设置参数:
try (Connection conn = connectionProvider.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql) {//resources of conn and pst
setPrepareStatementParameter(pstmt, kvs);//need to set parameters, so I have to put ResultSet into another try-with-resources statement
try (ResultSet res = pstmt.executeQuery()) {
..............
}
}
问题:
是否将 ResultSet 放入一个单独的 try-with-resources 语句中,因为文档声明关闭Statement
将关闭 ResultSet