aspose file tools*
The moose likes JDBC and the fly likes Accessing ResultSet after closing Statement/Connection gives an Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Accessing ResultSet after closing Statement/Connection gives an Exception" Watch "Accessing ResultSet after closing Statement/Connection gives an Exception" New topic
Author

Accessing ResultSet after closing Statement/Connection gives an Exception

Sudipto Ghosh
Greenhorn

Joined: Jan 07, 2005
Posts: 11
If I get a ResultSet object from Statement and close the Statement and Connection before and then try to access the ResultSet, it gives me an Exception.
Now, the question is how it is implemented from coding perspective?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

How is what implemented? When you ask for data, it gets read from the database. If you're not connected to the database, you can't get the data.

If you need this functionality, have a look at CachedRowSet in the JDBC extensions.
Sudipto Ghosh
Greenhorn

Joined: Jan 07, 2005
Posts: 11
Thanks for replying David ...
I will have a look into CachedRowSet. Meanwhile, just wanted to know, when we get the ResultSet, is the ResultSet object is populated with values at that time or, whenever we access it ( say, resultSet.next() ) it goes to DB and fetches the data?
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I know this doesn't completely answer your question, but my understanding is that the Statement needs to remain open until you're done with the ResultSet. Usually we have an object that's responsible for interacting with the database, so when an object needs to load data into a ResultSet, it creates that database handler object, gets a ResultSet and uses it and closes it, and then it closes that database handler object (meaning it calls a method that tells the database handler to close the still open Statement object).
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Sudipto Ghosh:
when we get the ResultSet, is the ResultSet object is populated with values at that time or, whenever we access it ( say, resultSet.next() ) it goes to DB and fetches the data?
It depends on the driver as it would be possible to load the whole result set if you had enough RAM. However, in general the result set has a handle to an open cursor on the database that is used to retrieve data as you call next(). Imagine executing a query that returns 200 million rows of 40 columns each (I know, who does this, yadda yadda). As well, you may only need the first row, so reading all data ahead of time would be wasted effort in some cases.

Long story short, close the ResultSet, then close the Statement and Connection if you're done with them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing ResultSet after closing Statement/Connection gives an Exception
 
Similar Threads
Unit testing is so hard to understand for me!
Insertion Of Data
Accessing Connection pool
Something wrong in returning ResultSet
Hibernate Connection Pool