Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- creating multiple ResultSets out of same Statement object- will it have any effects?

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 ?

like image 718
Arun Avatar asked Dec 26 '22 21:12

Arun


2 Answers

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.

like image 182
Denys Séguret Avatar answered Jan 05 '23 15:01

Denys Séguret


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.

like image 42
Sankar Avatar answered Jan 05 '23 15:01

Sankar