File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problems using the SQL statment SUM()" Watch "Problems using the SQL statment SUM()" New topic

Problems using the SQL statment SUM()

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

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 ({
for(int j=1; j<=rs.getMetaData().getColumnCount(); j++)
{ System.out.print( rs.getObject(j)+ "\t");}
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.
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, ..."
You could also check to see what the actual column name is using the ResultSetMetadata, but you can't go wrong using AS
I agree. Here's the link:
subject: Problems using the SQL statment SUM()
It's not a secret anymore!