This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Kubernetes in Action and have Marko Luksa on-line!
See this thread for details.
Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems using the SQL statment SUM()  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi
I am accessing an SQL data using JDBC. The SQL query I'm making is:
ResultSet rs = stmt.executeQuery("select sum(OHScan), sum(Keyed), sum(HHScan), sum(Dispatch), sum(Stray), sum(Misc) from Induction where ((InductionName = 'IU011' or InductionName = 'IU014') and (EntryDate = '10-10-01 19:00:00' or EntryDate = '10-10-01 20:00:00'))");
The query returns the vaules it has to and I can print out the values in the DOS promt using:
while (rs.next()){
for(int j=1; j<=rs.getMetaData().getColumnCount(); j++)
{ System.out.print( rs.getObject(j)+ "\t");}
System.out.println();
}
But when I try to get the ResultSet printed out in a JTextArea in an application, using the rs.getInt("sum(OHScan)") function, it can not find the coloumn "sum(OHScan)". I know this coloumn does not exists, but is there any way I can get the number returned by the "sum(OHSCAN)" query, or do I have to take in all the entries in the "OHScan" coloumn and then add them together?
I'm not sure how good an explenation this was, but if you have any questions just ask.
Regards
S�ren Augustesen
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you'll have to use rs.getString(int); or use the keyword AS to specify the column name in your query:
"SELECT SUM(cost) AS sumthing, ..."
...
rs.getInt("sumthing");
You could also check to see what the actual column name is using the ResultSetMetadata, but you can't go wrong using AS
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!