jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes ResultSet and sum 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 "ResultSet and sum" Watch "ResultSet and sum" New topic

ResultSet and sum

Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130

I'm fairly new to JDBC and am trying to figure out if it's possible to use the results of sum(xxxx) in a ResultSet. I'm querying for "select sum(xxx) where xxx.....". But I can't figure out what to use to get the value of sum(). Can anyone tell me if this is possible?


Ken Januski
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

It will be the only column returned, so:

int result = resultSet.getInt( 1 );

(assuming it's an int).

You could also assign an alias to the column and get that:

SELECT SUM(xxx) AS result WHERE ...

int result = resultSet.getInt( "result" );

[Asking smart questions] [About Bear] [Books by Bear]
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Thanks much Bear,

I think I had a fundamental misunderstanding of ResultSet that thought each ResultSet.getXXX() referred to the columns in actual database not in the ResultSet, though this goes against all logic:-).

I think that either of your solutions should do the trick.

I agree. Here's the link: http://aspose.com/file-tools
subject: ResultSet and sum
It's not a secret anymore!