File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Row information from ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Row information from ResultSet" Watch "Row information from ResultSet" New topic
Author

Row information from ResultSet

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
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

Joined: May 26, 2003
Posts: 30580
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
GeeCON Prague 2014
 
subject: Row information from ResultSet