resultset error

Hello,

I seem to have a problem with the following code. It throws the following exception at the while statement if there is more

than one row returned from the first query:

java.sql.SQLException: Operation not allowed after ResultSet closed

try{

result = statement.executeQuery(query1);

String fileDescrip ="";

String user1 ="";

String lastname ="";

ResultSet rs =null;//result set for username

while (result.next()){

fileDescrip = result.getString(1);

user1 = result.getString("added_by");

rs = statement.executeQuery(query2);

if(rs.next()){

lastname = rs.getString("lname");

}else{lastname ="Unkown";}

rs.close();

}

}catch (Exception tpex){

System.err.println("err viewing taskplan " + tpex);

}finally{

result.close();

statement.close();

connection.close();

}

[1687 byte] By [maccarteea] at [2007-11-27 6:44:36]
# 1
Your resultset called rs has no data in it as far as I can see. If is equal to null.Did you mean to use result and not rs ?
Aknibbsa at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

java.sql.SQLException: Operation not allowed after ResultSet closed

The error message speaks for itself. If you close an resultset, then the action at the given line isn't allowed.

So, look where you're closing the resultset and ask yourself if it is the right place to close the resultset.

BalusCa at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> Your resultset called rs has no data in it as far as

> I can see. If is equal to null.

> Did you mean to use result and not rs ?

The first query is to get userid and other data from one table. Then I need to lookup another table to find the user's last name using the userid from the first query.

I cannot use result for the second query, since the remaining rows of the orginal query will be overwritten with the new rows of the second query.

The code works fine for the first result row, but throws the exception when it makes it back to the while loop.

I know I could combine both queries, but I want to keep them separate for error checking (If userid has no entry in user table, result row will be null).

maccarteea at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
> The code works fine for the first result row, but> throws the exception when it makes it back to the> while loop.Indeed. Your code confirms it. Re-read it and fix it.
BalusCa at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Found that you cannot use the same statement object unless your done with the orginal resultset. Created a new statement object and works now.
maccarteea at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
This is just a nasty workaround. You're closing the resultset *inside* the while loop, which caused the resultset being inaccessible in the subsequent loops.
BalusCa at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

> This is just a nasty workaround.

>

> You're closing the resultset *inside* the while loop,

> which caused the resultset being inaccessible in the

> subsequent loops.

I commented out both close statements before I tried the new statement instance. Didn't work. Besides, I was closing the second result set, which only contained one row.

maccarteea at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Declare the 2nd resultset inside the while loop.
BalusCa at 2007-7-12 18:16:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...