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 How to get a row count Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to get a row count" Watch "How to get a row count" New topic

How to get a row count

Dan Mortimer

Joined: Jun 01, 2004
Posts: 18

I'd like to be able to query a database and return the number of rows in a specific table. Here's the method I'm using to perform the query:

public ResultSet doQuery(Connection conn, String query) {
ResultSet results = null;

Statement qState = conn.createStatement();
results = qState.executeQuery(query);
catch(SQLException sqle)
System.out.println("SQL Exception:");

return results;

As you can see, I'm passing in a Connection object, and the query I'm passing in as a String is as follows:

"select count(*) from semServer"

Calling the method works just fine and I get a ResultSet object returned. I have used a ResultSetMetaData object to discover that my ResultSet object has one column (which is what I'd expect).

I have tried to use the ResultSetMetaData to get the name of the column, but all I get is an empty String. So, my problem is how do I access the result of my query contained within my ResultSet object?

Thanks in advance for your advice...

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

int rowCount = results.getInt(1);
[ December 14, 2004: Message edited by: Sonny Gill ]

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Dan Mortimer

Joined: Jun 01, 2004
Posts: 18
Hi Sonny,

Thanks for your help. I can't believe I missed something that was so obvious!

Thanks again,

I agree. Here's the link:
subject: How to get a row count
It's not a secret anymore!