aspose file tools*
The moose likes JDBC 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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to get a row count" Watch "How to get a row count" New topic
Author

How to get a row count

Dan Mortimer
Greenhorn

Joined: Jun 01, 2004
Posts: 18
Hi,

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;

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

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...

Dan
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
Consultant @ Xebia. Sonny Gill Tweets
Dan Mortimer
Greenhorn

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,

Dan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get a row count