File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Counting number of rows in resultset" Watch "Counting number of rows in resultset" New topic

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
author & internet detective

Joined: May 26, 2003
Posts: 32844

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
David O'Meara

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:
subject: Counting number of rows in resultset
It's not a secret anymore!