• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

open Resultset and closed connection

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think when we do next() , it works on local object and does not go to database. But we can't iterate through Resultset after closing connection. why can't next() works after closing connection??
Regards,
Murali
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murali Mohan Mohan Murali:
why can't next() works after closing connection??


Because your assumption is incorrect. ResultSet does not copy all the rows returned by a SQL query, it requests some number of rows from the database as they are needed (the get/setFetchSize() method returns/sets how many). Close the Connection object and ResultSet can no longer get rows from the database.
 
Murali Mohan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
ResultSet does not copy all the rows returned by a SQL query, it requests some number of rows from the database as they are needed (the get/setFetchSize() method returns/sets how many). */
I want to do next() operation on returned rows only.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:
it requests some number of rows from the database as they are needed (the get/setFetchSize() method returns/sets how many).


Therefore make the fetch size large enough to hold them before closing the Connection.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must get ResultSet object before closing the Connection then close the Connection and do whatever you need to do with resultset you acquired:
ResultSet rs=statementEx.executeQuery(sql_statement);
connection.close()
while(rs.next()){
// your processing here
//
}

I think when we do next() , it works on local object and does not go to database. But we can't iterate through Resultset after closing connection. why can't next() works after closing connection??
Regards,
Murali[/QB]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Murali,
That 's just the way JDBC works: the ResultSet depends on the Connection. If you close the Connection, the ResultSet is closed as well. We, simple souls uisng the stuff, can try and understand the reasons behind this, but that effort will not change the facts. That 's what the JDBC specification said. Period.
Good riding,
Rudy.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a look though the JDBC API Tutorial and Reference (link to the bunkhouse review) and it also pointed out the javax.sql.RowSet from the JDBC 2.0 extension. It should be available bundled with J2SE 1.5, but you can also look here to get it as a separate download.
An extension of the RowSet is the CachedRowSet, which allows you to wrap a ResultSet with a RowSet and makes the data available after the Connection is closed. It is also useful since it can decorate non-scrollable ResultSets with 'scrollability'. It's worth reading about the other implementations at the same time, it kept me up quite last night...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic