Welcome!

Java Authors: Roger Strukhoff, Bernard Golden, Harald Zeitlhofer, Elizabeth White, Jnan Dash

Related Topics: Java

Java: Article

How do I find out how many records are in a RecordSet?

How do I find out how many records are in a RecordSet?

If you already have your ResultSet, you have two choices, both bad: one is to keep a counter as you read the records in the ResultSet, and the other is to hope you have a compliant JDBC driver that supports the getRowCount() method. Both ways probably do the same thing: read the entire dataset. If you're interested in only a count of records, that's a lot of bandwidth down the drain (and memory, too, if you happen to need the data as you fly past it.)

A better approach is to run a separate query to determine the count of rows:

PreparedStatement ps=connection.prepareStatement("select count(*) from tablename");
ResultSet rs=ps.executeQuery();
rs.next();
int count=rs.getInt(1);

Note that if your query is complex enough, for some databases this will actually be slower than counting records as you iterate over the ResultSet.

If you've managed to set up a scrollable ResultSet, the following code might also work. Your author hasn't used it, as row counts haven't proven useful to him for some reason.

public static int getRowCount(ResultSet rs) throws SQLException {
   int current = rs.getRow();
   rs.last();
   int count = rs.getRow();
   if(count == -1)
       count = 0;
   if(current == 0)
       rs.beforeFirst();
   else
       rs.absolute(current);
   return count;
}

Reproduced with permission of http://java.enigmastation.com/index" The Undernet #Java Knowledge Base

More Stories By Joseph Ottinger

I am a software evangelist for GigaSpaces technologies, as well as a writer and musician. I've been the editor-in-chief of Java Developer's Journal and TheServerSide.

GigaSpaces Technologies is a leading provider of a new generation of application platforms for Java and .Net environments that offer an alternative to traditional application-servers. The company's eXtreme Application Platform (XAP) is a high-end application server, designed to meet the most demanding business requirements in a cost-effective manner. It is the only product that provides a complete middleware solution on a single, scalable platform. XAP is trusted by Fortune 100 companies, which leverage it as a strategic solution that enhances efficiency and agility across the IT organization.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.