Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Row information from ResultSet

 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic