wood burning stoves 2.0*
The moose likes JDBC and the fly likes SELECT count(*) FROM ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SELECT count(*) FROM ..." Watch "SELECT count(*) FROM ..." New topic
Author

SELECT count(*) FROM ...

roni suvanto
Greenhorn

Joined: Jul 23, 2002
Posts: 13
I have a bean that handles all my database connections/queries and can't quite figure out how to get results from "SELECT count(*) FROM.." queries displayed on .jsp page.

Normally I use it like this:



and then request data with rs.getString("something")/rs.getInt("something) etc.

But how do I get the count when using "SELECT count(*) FROM"-statement ?

rs.getXXX("xxx") ?
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
You still use the rs.getInt() method, but instead of passing in the column name, you use the column index. Since a SELECT count(*) query returns only a single column, the index is 1, like this:
rs.getInt(1)
[ June 10, 2004: Message edited by: Blake Minghelli ]

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Alternatively, you give the column a name using an alias:

SELECT COUNT(*) AS COUNT FROM MYTABLE WHERE...

int count = rs.getInt("COUNT");

I assume that you know that embedding such database code in your JSP is bad practice... if you really need to, at least use the JSTL SQL tags.

- Peter
 
 
subject: SELECT count(*) FROM ...
 
Similar Threads
Why use Map.entrySet()
sql question
Writing a query that uses the current date
Finding the total row of my table.
Best Practice for Listing Directory Names