*
The moose likes JDBC and the fly likes Finding out the number of rows a database has. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Finding out the number of rows a database has." Watch "Finding out the number of rows a database has." New topic
Author

Finding out the number of rows a database has.

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I wanted to ask about retrieving the number of rows from ResultSetMetadata(ie, to find out the number of rows a particular database has- the one that is being connected to). I know that getCoulumnCount tells you the number of columns the table has but how would I find out the number of rows because as far as I can see there is no getRowCount method as such? Plaese help!
Nicola Zuliani
Greenhorn

Joined: Feb 26, 2004
Posts: 4
Did you try with a "while" which increments a variable?
...or with a query with a COUNT(*)...
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Do you mean something like while there is another row increment some counter?
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Originally posted by Naf Rash:
I wanted to ask about retrieving the number of rows from ResultSetMetadata(ie, to find out the number of rows a particular database has- the one that is being connected to). I know that getCoulumnCount tells you the number of columns the table has but how would I find out the number of rows because as far as I can see there is no getRowCount method as such? Plaese help!

You can use the RowSet Interface which extends java.sql.ResultSet It has a method called getMaxRows().


Thanks, leo
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I have tried the method getMaxRows but I have read that it:
The maxRows limit is the maximum number of rows that a RowSet can contain.

Therefore shouldn't the limit be set first? I have tried to use this method on an object of ResultSet but it isn't being recognised for this. Can you suggest why???
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Originally posted by Naf Rash:
I have tried the method getMaxRows but I have read that it:
The maxRows limit is the maximum number of rows that a RowSet can contain.

Therefore shouldn't the limit be set first? I have tried to use this method on an object of ResultSet but it isn't being recognised for this. Can you suggest why???

You're right, scratch that last comment.
Try this:
ResultSet rs = stmt.executeQuery("Select whatever from whatever");
int count = 0;
while(rs.next()){
count++;
}
Just like Nicola suggested.
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100



An egg is a chicken's house!
Denna Ramani
Greenhorn

Joined: Feb 28, 2004
Posts: 2
hey there,
the easy solution is
use this query "select count(*) form tablename;"
and then read the resultset with getInt(). so it will return number of rows in particular table.
Denna
 
jQuery in Action, 2nd edition
 
subject: Finding out the number of rows a database has.
 
Similar Threads
how to get num of rows in ResultSet
Selecting n records from a table
getFetchSize().... what is it for
Getting the number of rows from a database
How to obtain a database table size?