Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / July 2003

Tip: Looking for answers? Try searching our database.

2 queries (Resultset)

Thread view: 
Max - 21 Jul 2003 21:03 GMT
hi,

In a method, I need to execute 2 queries. The arguments of the second one
are the results of the 1st one:

String query1 = "Select A from table1";  // A = SQL_CODE
String query1 = "Select Z, Y from table1 where A = code";
PreparedStatement ps = conn.prepareStatement(query1);
PreparedStatement psa = conn.prepareStatement(query2);

String code;

ResultSet rs = ps.executeQuery();
while(rs.next()){
code = rs.getString(SQL_CODE);
}
// output can be several lines

how do i do to pass "code " as a parameter to my 2nd query ?

thanks
Andy Flowers - 21 Jul 2003 22:47 GMT
String query1 = "Select A from table1";  // A = SQL_CODE
String query1 = "Select Z, Y from table1 where A = ?"; // note the question
mark
PreparedStatement ps = conn.prepareStatement(query1);
PreparedStatement psa = conn.prepareStatement(query2);

String code;

ResultSet rs = ps.executeQuery();
while(rs.next())
{
   code = rs.getString(SQL_CODE);
   psa.setString(1,code); // set the value required
   psa.executeQuery();
}

better still combine the SQL into one statement wih a sub-query

String query1 = "Select Z, Y from table1 where A in (Select A from table1)";

> hi,
>
[quoted text clipped - 17 lines]
>
> thanks
Heinz Huber - 23 Jul 2003 08:40 GMT
> String query1 = "Select A from table1";  // A = SQL_CODE
> String query1 = "Select Z, Y from table1 where A = ?"; // note the question
[quoted text clipped - 11 lines]
>     psa.executeQuery();
> }

I wouldn't recommend this without using two different connections.
AFAIK, you can only have 1 open resultset per connection. Therefore, the
second executeQuery invalidates the first resultset and it might stop
working or produce incorrect results.

> better still combine the SQL into one statement wih a sub-query
>
> String query1 = "Select Z, Y from table1 where A in (Select A from table1)";

This is the way to go!

Regards,
Heinz
Andy Flowers - 23 Jul 2003 18:27 GMT
<<SNIP>>

> I wouldn't recommend this without using two different connections.
> AFAIK, you can only have 1 open resultset per connection. Therefore, the
> second executeQuery invalidates the first resultset and it might stop
> working or produce incorrect results.

Not true. A Connection has nothing to do with ResultSet, that's the job of
the Statement

A Statement can only have one active result set at any one point in time.

In summary

Connection has 0..many active Statement has 0..1 active ResultSet

<<SNIP
Avi Abrami - 22 Jul 2003 01:12 GMT
> In a method, I need to execute 2 queries. The arguments of the second
> one are the results of the first one:
[quoted text clipped - 13 lines]
>
> how do i do to pass "code " as a parameter to my 2nd query ?

Max,
It appears to me that you have several typing mistakes in the code
you posted. I assume that you wish to "SELECT" data from two
different tables. Here is what I think your SQL query should look
like (based on my assumptions):

select Z, Y from table2 where A in (select A from table1)

Good Luck,
Avi.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.