| By Joseph Ottinger | Article Rating: |
|
| January 1, 2000 12:00 AM EST | Reads: |
10,650 |
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 10,650
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
Joseph Ottinger, formerly editor-in-chief of JDJ (2003-4), is a consultant with Fusion Alliance in Indianapolis and is one of the contributors to the OpenSymphony project.
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Journal Opens "Readers' Choice Awards" Nominations
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Industry Experts Discuss the State of Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- It's the Java vs. C++ Shootout Revisited!
- The End of IT 1.0 As We Know It Has Begun
- An Introduction to Abbot
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- How to Diagnose Java Resource Starvation
- REA Is Where RIA Becomes the Norm
- Kindle 2 vs Nook
- Anatomy of a Java Finalizer
- Why IBM’s Server Chief Got Busted
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- 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
- What's New in Eclipse?





























