File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes SELECT count(*) FROM ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "SELECT count(*) FROM ..." Watch "SELECT count(*) FROM ..." New topic

SELECT count(*) FROM ...

roni suvanto

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:
[ 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
Ranch Hand

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


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
I agree. Here's the link:
subject: SELECT count(*) FROM ...
It's not a secret anymore!