Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding out the number of rows a database has.

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try with a "while" which increments a variable?
...or with a query with a COUNT(*)...
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean something like while there is another row increment some counter?
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Denna Ramani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic