This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Count total number of records in database 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 "Count total number of records in database" Watch "Count total number of records in database" New topic
Author

Count total number of records in database

Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
How do you count the total number of records in your database.
Probably something simple query called COUNT.
Could someone tell me the syntax for this, and how do you then display the result of your count?
Many thanks.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Ok, for a single table, to count the number of rows you issue:
select count(*) from mytable
But to count the number of rows in your entire database (do you really need to know this?), you first need to determine the tables, and then issue the same select statement above.
To find the names of tables in a database is usually vendor specific, eg. in oracle there are numerous ways to do it, but a common one is:
select * from cat
Also to keep in mind, depending what user you used to connect to the database, may affect which tables you have access to.
Also, in JDBC, there is a method on the java.sql.Connection interface to retrieve the metadata for the database (getMetaData()), and in the java.sql.DatabaseMetaData interface you should find some methods for retrieving the tables within the database.
So the pseudo code would be:


In case I have gone way off the mark, and you just need code for rows in 1 table:

[ March 05, 2004: Message edited by: James Swan ]
Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
Thank you. I just needed the number of rows in onw table.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Count total number of records in database
 
Similar Threads
Row counts with Hibernate Criteria
About Urly Bird 1.2.0
Increasing performance through indexing
how to delete huge amount of records
Using Struts to iterate over queries with aggregates