I need to run several queries in a row
Statement st = cnx.createStatement();
ResultSet rs = st.executeQuery( "SELECT [good stuff]");
// do something smart with rs
rs = st.execute( "SELECT [better stuff]");
// do something smarter with rs
rs = st.execute( "SELECT [best stuff]");
// you got it
try{ rs.close();} catch( SQLException ignore){};
try{ st.close();} catch( SQLException ignore){};
Is this a problem that the first two ResultSet are not properly closed or is it implicitely done during garbage collection?
As soon as you execute the 2nd query, the previous
ResultSetis automatically closed. And as far asGarbage Collectionis concerned, you don’t have to worry about that. You can just have astmt.close()at the end that’s all. It will automatically close all the relatedResultSetobjects.Take a look at : –
ResultSet#closedocumentation, which says that: –If you want to test, whether your resultset gets closed or not, you can use a while loop to iterate over the
result setand inside the while loop, create another query and assign it to same result set. You will see that an Exception will be thrown..So, in the above code, on the 2nd iteration, you will get an
Exception: - Cannot perform operation after ResultSet is closed