1

How to execute a Query with the previous Execute query result While loop.

Resultset rs = con.excuteQuery(Query1.toString);
While(rs.next()){
  Resultset rs1 = con.excuteQuery(Query2.toString);
  while(rs1.next()){
  }
}

once the second Query executed then previous rs can not be executed. For Ex: 1st Query have 2 records if the second query executed then the 1st Qry did not fetch the 2nd record.

please give the solution as soon as possible.

thanks.

A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
Babu R
  • 1,025
  • 8
  • 20
  • 40
  • 1
    here is a similar post http://stackoverflow.com/questions/935511/how-can-i-avoid-resultset-is-closed-exception-in-java . For myself, I agree with those who say this nested loop should have been in the query – A.B.Cade Jun 22 '12 at 07:15

2 Answers2

0

Presumably in your example con is a statement; the javadocs state:

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

So you need two separate statements.

Edit: I agree with @A.B.Cade and the previous answer though, you should be thinking in sets and letting the database do the joining so you have a single result set. If you're currently doing work using the data from the first result set, you just need to keep track of whether you've seen that data before, e.g. by tracking the last value seen for one or more columns.

Community
  • 1
  • 1
Alex Poole
  • 183,384
  • 11
  • 179
  • 318
0

As mentioned in comments, if your two queries are related then you should merge them into one using an inner join. Otherwise, your options depend on the database and/or driver.

1) use two different Connections to execute your queries

Or:

2) [If your driver allows it] use two PreparedStatements instead of just executing a query directly on the connection.

I would recommend you get into the habit of using prepared statements unless you have a very good reason not to. They have all sorts of benefits over just executing queries directly over the connection.

KidTempo
  • 910
  • 1
  • 6
  • 22