This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!