| By Joseph Ottinger | Article Rating: |
|
| January 1, 2000 12:00 AM EST | Reads: |
15,733 |
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
Published January 1, 2000 Reads 15,733
Copyright © 2000 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?

















