wood burning stoves 2.0*
The moose likes JDBC and the fly likes Executing one resultset inside another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Executing one resultset inside another" Watch "Executing one resultset inside another" New topic
Author

Executing one resultset inside another

Nilesh Soni
Greenhorn

Joined: May 14, 2001
Posts: 21
Hi all,
My problem is something like this. I have a Master table and a Transaction table. I am executing a query which retrieve all the Ids from Master table and then as the Ids are getting from resultset i want to fire another query (while the first resultset is executing) which gets data from Transaction table if it has the record of same Id as of Master.
Coding is as follows:
query = "Select Id,Name from Master" ;
while(r.next()){
CompId = r.getInt("Id") ;
System.out.println(r.getString("Name")) ;
query = "Select Distinct(TransId) from Transaction where TransId = "+CompId ;
r1 = s.executeQuery(query) ;
while(r1.next()){
System.out.println(r1.getInt("TransId")) ;
}
}
while executing this the first resultset (r in our case) gets closed automatically. can anyone help me solving this problem.
Thank You.
Nilesh Soni
------------------
Julio Lopez
Greenhorn

Joined: Nov 14, 2000
Posts: 28
query = "Select Id,Name from Master" ;
while(r.next()){
CompId = r.getInt("Id") ;
System.out.println(r.getString("Name")) ;
query = "Select Distinct(TransId) from Transaction where TransId = "+CompId ;
r1 = s.executeQuery(query) ;
while(r1.next()){
System.out.println(r1.getInt("TransId")) ;
}
}
The first thing I would do is try a simple join as in:
query = "Select Id, Name, Distinct(TransId) from Master, Transaction where (TransId = Id)"
The reason the resultSet might be getting closed out is because it reaches the end of the rows selected for. Make sure you are getting the data you expect. Another reason might be that your JDBC driver is buggy.
Julio Lopez
M-Group Systems
[This message has been edited by Julio Lopez (edited June 07, 2001).]
Nilesh Soni
Greenhorn

Joined: May 14, 2001
Posts: 21
julio,
thanx for your efforts, but what i wanted was not so simple as you have understood. i wanted the rows of master tables also which has no similar records in transaction table. finally i got the solution by using left outer join. may be i was not able to write the exact solution what i wanted. but any way thanx.
bye
------------------
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Executing one resultset inside another