根据http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#close()文档的说明,当Statement对象关闭时,如果存在当前ResultSet对象,则也将被关闭。但是根据Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?的解释,明确地关闭Connection、Statement和ResultSet似乎是一个好习惯。如果我们仍然需要关闭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