This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Counting number of rows in resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Counting number of rows in resultset" Watch "Counting number of rows in resultset" New topic
Author

Counting number of rows in resultset

Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
Is there any way to count the number of rows in the result set.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

Glenny,
The two most common alternatives are to do a separate query of:
"select count(*) from ... " or to count the rows as you loop through the data. The first is better if you just need to know how many rows there are. The second is better if you need to loop through the data anyway.


[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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

A third possibility, if your Driver supports JDBC 3 is to use rs.afterLast() to move to the end and then rs.getRow() to get the row number. I always do it one of the ways listed by Jeanne.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Counting number of rows in resultset