• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capturing result of SUM query

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck trying to capture the output of the SUM
SQL command. I have a table called Coins with columns
Type, and Value. The exercise asks us to (among other things) use the 'SUM'
SQL command to get the sum of the Values column. Here
is the section of code that is giving me trouble.

The SimpleDataSource class just provides a convienent method for establishing a connection to the database. I
know there is data in the database because the rest of
my queries return predictable results. If needed, I
could attach my source code. I would be greatful for
any tips you can provide.

Thanks,
Garrett
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34654
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett,
That code looks fine to me. What does the query return if you just run:
"SELECT Values FROM Coins" ?

Obviously, this won't solve your problem, but it may narrow it down. Also, what database are you using?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.

The problem wasn't that

while (result.next())

was returning false, it was that I was trying to provide a column name when I was examining my ResultSet:


This is a snippet from the working code


Thanks for the help. While going back and trying the query you suggested, the light switch just flipped on.

btw I'm using HSQLDB
[ February 18, 2006: Message edited by: Garrett Rowe ]
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you'd prefer to get column by name, which is always easier than keeping your indexes straight as the code evolves, most databases/drivers will support this:

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip stu. That's one that wasn't in my text. That's exactly why I love this forum.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34654
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many databases require an "as" when definining a column name alias:
"String query = "SELECT SUM(Value) as SUM_VALUE FROM Coins";"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic