Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get a row count

 
Dan Mortimer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int rowCount = results.getInt(1);
[ December 14, 2004: Message edited by: Sonny Gill ]
 
Dan Mortimer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonny,

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

Thanks again,

Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic