Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Problems using the SQL statment SUM() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problems using the SQL statment SUM()" Watch "Problems using the SQL statment SUM()" New topic
Author

Problems using the SQL statment SUM()

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems using the SQL statment SUM()
 
Similar Threads
SEVERE: BAD packet signature 200,SEVERE: Caught exception (java.lang.NullPointerException)
Total Time duration
unable to retrieve timestamp in my servlet
Capturing result of SUM query
unable to retrieve timestamp in my servlet