5

I have a 2 ResultSet's generated from the same Statement object.

Code Sample follows:

Connection con            = null;
Statement  stmt           = null;
ResultSet rs = null;

con = DBAccess.getConnection();
stmt = con.createStatement();

rs = stmt.executeQuery(Query1);
// operate on the resultset

rs = stmt.executeQuery(Query2);   // Is it legal and do not have side-effects?
// operate on the resultset

// close everythings (Resultset, Statement, Connection)

I checked it to work well. My doubt is will it have any side effects ?

3 Answers 3

10

From the javadoc :

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

So yes, you can do it safely. You just can't use your first resultset after you have executed the second query.

Sign up to request clarification or add additional context in comments.

Comments

2

There will be only one Resultset object for each and every statement object. So, When you execute another query, internally it will close the existing resultset object and opens a new resultset object. But you cannot access the previous resultset obejct.

There will be no side effects. you can use it as you wish.

Comments

0

Remember to close the first resultset before using the same variable to reference the second resultset. If you close the resultset only after the execution of the second statement, the second resultset is closed and the first resultset is not closed.

1 Comment

But in above answer dystroy quotes from Javadoc that "All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists." So it should be auto closed Right ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.