Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic