aspose file tools*
The moose likes Servlets and the fly likes select count(*) from Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "select count(*) from Table" Watch "select count(*) from Table" New topic
Author

select count(*) from Table

Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
How can I findout how many records are in a table via jsp. Can I just use:
ResultSet bc = stmt.execute(select count(*) from Table)
If so, how can I acces the value if there is no field name?
Adam Chace
Greenhorn

Joined: May 24, 2001
Posts: 14
Just do:
Select count( field ) as ct from table
where "field" is the name of the primary key. Then you can use "ct" as the fieldname for the count.
------------------
Adam Chace
Author of :JSP Tag Libraries
Chalk Creek Software


Adam Chace<BR>Author of :<A HREF="http://www.amazon.com/exec/obidos/ASIN/193011009X/electricporkchop/102-2552103-3190518" TARGET=_blank rel="nofollow">JSP Tag Libraries</A><BR><A HREF="http://www.chalkcreek.com" TARGET=_blank rel="nofollow">Chalk Creek Software</A>
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Thank you!...I dont know why I didn't think of that myself.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ronnie Phelps:
Thank you!...I dont know why I didn't think of that myself.

First, your question. There is no field name, but nothing stops you from accessing the value by index - resultSet.getInt(1) should do fine.
Second, the semantics of the SELECT COUNT(field) query. When you use COUNT(*), you count all selected records in the table. When you use COUNT(field), you only count those selected records where field is not NULL. If this field is (part of) the primary key, or if it has a not-NULL constraint for another reason, you are OK. If you don't have suitable non-NULL fields or don't want to make fairly obscure assumptions in your code, refer to the result by index instead of name.
- Peter

[This message has been edited by Peter den Haan (edited June 27, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: select count(*) from Table
 
Similar Threads
createQuery() return types
sql script works in mssql but not in mysql
how to write this query... count, unique, ??
how to find all tablename and rowcount of each table from database?
howto get total of rows from mysql