Saturday, March 04, 2006

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

java.sql.SQLException: Operation not allowed after ResultSet closed .
I thought that this exception worth posting here, since when i struck up with this exception, i tried to search in many of the java forums, but they haven't provide me the alternative solution to this problem. Anyhow, as an introduction to this problem, i am writing an application in which the a user will be logged in to the system with his user-id and is able to pick up one of the roles assigned to him at a particular time. So i thought that without writing the two seperate servlet programs i ended this within one program using both doGet() and doPost() methods. But when i required to show the roles available to the particular user, i required to connect to the database again { previously i connected to check the user credentials, and i am still in that loop}, i had created one more resultSet and inserted a stament like:
rs1=stmt.executeUpdate("select roleName from userroles where user=' "+user+" ' ");
compiled successfully, but when i tried to run it, i gives me exception that "Operation not allowed after ResultSet closed", where i am trying to use the same statement connection. I later thought that i got this exception because of lack knowledge of JDBC drivers. In any application we have to connect to only one Statement to the ResultSet and if we want to connect again we need to use PreparedStatement Object, instead of the Statement Object. And to have more connections we need to instantiate CallableStatement Objects. This resolves me the problem of connecting to the database with as many statements as required at the same time.
So i used the following code fragment:
..............
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeUpdate("select userName, password from users where userName=' "+user+" ' ");
PreparedStatement pstmt=conn.prepareStatement("select roleName from userroles where userName=' "+user+" ' ");
ResultSet rs1=pstmt.executeQeury();
...
while(rs.next()){
.....
while(rs1.next()) {
....
}
}
.........
This way of programming solves me the problem. Though i am trying to look at the other alternatives if any.

21 comments:

Anonymous said...

Thanks man,

that was of great use prolly saved me a lot of time :)

keep up the good work

Anonymous said...

MAn!! tanks!!

Anonymous said...

many thanx for your tipp. Without your article, i would still be searching for a solution for that problem..

Anonymous said...

me too -- excellent tip!

Anonymous said...

thank you, you help me for my exam i was in panic! :-)

Boonpay Mok said...

dude, really thanks a lot!!!
u r brilliant!!!

Unknown said...

Thanks a lot Umamaheswara

Kenn said...

Thanks a mil. really saved me huge amount of time

veena said...

Ya this is right,
but can v do by creating two ResultSet objects,and using for different queries????

ResultSet resultset=stat.executeQuery("Select * from Employee");

and

ResultSet resultset1=stat.executeQuery("select * from Employee");

Anonymous said...

Thank you..........
It worked.........
It saved a lot of time.....
Keep going.........

Anonymous said...

i spended several hours trying to figure out what was wrong. i try that solution, and now works perfectly.

about the Veena sugestion, that was one of my first solutions but that didn't solve the problem.

Anonymous said...

thank you....

Unknown said...

Thx a lot.

Rene R Ramirez S.
Blog: http:\\developyourdream.blogspot.com

Anonymous said...

Thanks Uma. it is very useful.
Vijay

Anonymous said...

Thank you! It sure helped me a lot. Respect!

Anonymous said...

Thank you! Very helpful!

Anonymous said...

Thanks man...
It worked...
You saved a lot of my time

Poojan Sharma said...

thanx alot friend, i was really having a difficult time with this exception, try alot to find a solution for this problem but couldn't find one till date, finally came through your post and could successfully remove the exception, once again thanx alot

kriss said...

u sure saved my ass... wasted 4 hrs thinkin how to make it work...

Anonymous said...

Thanks, Thanks,Thanks,Thanks,Thanks,Thanks,Thanks,Thanks :) you saved me :)

Anonymous said...

thankyou sooooo much for this sol!

Post a Comment

Drop your message here to get in touch with me