Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing ResultSet after closing Statement/Connection gives an Exception

 
Sudipto Ghosh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic