• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get a row count

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic