I have a CachedRowSet
object in Java. Since CachedRowSets
are disconnected from the DB, is there a need to close them, or put them in try-with-resources blocks? My IDE is suggesting I should.
Here is how I am using it, I fill the CachedRowSet
from a query (and use try-with-resources for the PreparedStatement
and ResultSet
objects)
public static void resultSetParsing(Connection con) throws Exception {
String sqlStatement = " select id, name from TestTable ";
CachedRowSet cachedRowSet = null;
try(PreparedStatement pstmt = con.prepareStatement(sqlStatement)){
try(ResultSet rs = pstmt.executeQuery()){
cachedRowSet = RowSetProvider.newFactory().createCachedRowSet();
// Fill the CachedRowSet from the ResultSet
cachedRowSet.populate(rs);
}
}
// I am disconnected from the DB at this point
while(cachedRowSet.next()){
System.out.println("Found item: " + cachedRowSet.getString("id") + " " + cachedRowSet.getString("name"));
}
}
Does it matter that I am not closing it at the end? I want to use this object properly.