Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet and sum

 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" );
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic