File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Multiple Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Multiple "select" in one query" Watch "Multiple "select" in one query" New topic
Author

Multiple "select" in one query

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi all
Im connecting to a MS SQL database 7.0 using JDBC. I have to make the same query multiple times with the only change being a new "where" clause. Im trying to do this with a query that looks like this:
queryString= "select sum(ITEM) as item from database where condition1; select sum(ITEM) as item2 from database where condition2;";
and Im getting the result of the query out in to integers like this:
itemInt = rs.getInt(item);
item2Int = rs.getInt(item2);
This works fine if I only have 1 "select" query in the queryString, but when I put in the second "select" query I get an SQLException that: Column "item2" not found.
Do I have to make seperate queryStrings for each timeperiod or can it be done in one??
If you need to see the code just ask.
Regards
S�ren Augustesen
[This message has been edited by S�ren Augustesen (edited December 15, 2001).]
[This message has been edited by S�ren Augustesen (edited December 15, 2001).]
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
You need to make separate query strings. There is a way to do multiples - you can write a stored procedure that will do the multiple SQL statements and use CallableStatement. But it looks like here you want 2 query strings.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Jim
Thanks for the quick answer, I'll give it a try.
Regards
S�ren Augustesen
Vladimir Ergovic
Ranch Hand

Joined: Apr 22, 2001
Posts: 63
You should try combination with union of selects.


Vladimir Ergovic
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple "select" in one query
 
Similar Threads
Problems using the SQL statment SUM()
RMI HELP -- URGENT
ORA-01009: missing mandatory parameter
Why do these queries break this loop????? Im lost!
Putting a JTabbedPane in a JInternalFrame