This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 60774
    
  65

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResultSet and sum
 
Similar Threads
The BarTenders Kids
Timer
Getting data from database into a collection
who can my question about this code
addAll