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 row-count out from the ResultSet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to get row-count out from the ResultSet?" Watch "How to get row-count out from the ResultSet?" New topic

How to get row-count out from the ResultSet?

Jorma Ikonen
Ranch Hand

Joined: Sep 17, 2001
Posts: 49
I'm just wondering how I can read the number of rows (records) in DB...
ResultSet res = smnt.executeQuery("SELECT count(*) FROM table_name");
what should I do next? I mean that next may not work:
String count = res.getString("???");
I'm also interested in that what is the "official" way to give an unique ID-number for each record. Do I have to give it, or does the DB-server do the job?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Or, if you prefer,About ID numbers: you can actually write a whole article about this. There is no out of the box facility to generate ID codes in SQL, but most databases have their own facilities for the purpose. For example, in Oracle you'd use sequences; in SQL Server you'd use identity columns. If you need compatibility with multiple databases, then you either use an old-fashioned "nextnum" type table, or abstract out the database-specific bits into an interface which is implemented separately for each database (data access frameworks such as Castor usually go this route).
- Peter
I agree. Here's the link:
subject: How to get row-count out from the ResultSet?
It's not a secret anymore!