• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

select count(*) from Table

 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!...I dont know why I didn't think of that myself.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
reply
    Bookmark Topic Watch Topic
  • New Topic