wood burning stoves*
The moose likes JDBC 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
Bookmark "ResultSet and sum" Watch "ResultSet and sum" New topic
Author

ResultSet and sum

Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Hi,

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?

Thanks,

Ken Januski
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

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] [Bear's FrontMan] [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.

Ken
 
Don't get me started about those stupid light bulbs.
 
subject: ResultSet and sum