• 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

Row information from ResultSet

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am using ResultSet to fetch the returned records from the database. How can I get the number of rows directly?

I have used the following
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //for scrollable result set.
rs=stmt.executeQuery("SELECT * FROM tab");
int k=rs.getFetchSize();

But it is not working. I am aware of some methods to find the rows fetched like keeping count and travesing rs using rs.next() or quering to database using Count key word in the query to get the records.

But I want to get the size directly. Any help on this issue would be appreciated.

Regards,
Kiran.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ResultSets can and usually do implement lazy fetching from the database server. That is, they don't have to get all the rows into memory at once. So they can't tell how many rows there are until they get them all. You can try afterLast() and getRow() in some combination, but you'll have to make the rs scrollable in both directions (as you did) to go back and read any data. That will force the rs to hold all data in memory at once, which may be a problem.

Using another query to get the number of matching rows might be good. Of course the count might change between the count query and the real query. My SQL is rusty ... select count(any column) with no where clause?
[ June 26, 2007: Message edited by: Stan James ]
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stanley for the solution but I am looking for a solution in which we should not even scroll the result set and not fire one more query to fetch the number of records.

Regards,
Kiran.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kiran,
The fetch size isn't the total count of the result set. It's the number of records returned in each network trip.

Originally posted by K Kiran Kumar:
I am looking for a solution in which we should not even scroll the result set and not fire one more query to fetch the number of records.


There isn't one. You need to loop through the result set or do a separate query.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic