jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes JDBC ResultSet closing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC ResultSet closing" Watch "JDBC ResultSet closing" New topic
Author

JDBC ResultSet closing

Mitch Young
Greenhorn

Joined: Dec 15, 2006
Posts: 10
Hello Java Ranch-A-Rareos!
I'm querying a mySql db to see if a user that is trying to register is already registered.
I query for the email and use the count function as you can see below: If it returns 0, the user gets registered, if 1, it returns the boolean. What is happening is that if the user is not in the db and the stmt.executeUpdate(insert); executes, when control passes back to the while test if fails because the ResultSet has been closed. Does anybody know how the ResultSet is being closed? Thanks for your help.


K Amit Gupta
Greenhorn

Joined: Feb 22, 2011
Posts: 4

Hi,
Why are you using while?
your objective is to simply check that user exists in database or not.
if your queryEmail is returning 0 it means user is not register and you want to run insert query for that user.
and after runnning insert query, why you want to run insert for same user?

and your resultSET would be TYPE_FORWARD_ONLY by default...

we can't back in result set if it is TYPE_FORWARD_ONLY ... actually your result set is not closing but it reaching END of result set.

you can use ResultSet.TYPE_SCROLL_SENSITIVE to iterate both side in result set.


Thanks!!!
Amit
Mitch Young
Greenhorn

Joined: Dec 15, 2006
Posts: 10
Amit,
Thanks so much, I removed the while loop and it all works as expected now.

I'm not trying to re-register the same user, only check when a user registers if they are already registered and forgot.

I still don't understand why the second time through the loop it was throwing a nullPointerException and a
"SQLEception: Operation not allowed after ResultSet closed" . If it is the end of the ReultSet won' t a call to next() return false and pass control out of the loop? But anyway, it seems that somehow the ResultSet was being closed throwing the nullPointerException.
Thanks again.
m coffee
Greenhorn

Joined: Nov 10, 2010
Posts: 5
It's your stmt.executeUpdate in the middle of reading from the resultset.

One way that works is to open 2 statements, and use the second one just for the updates.
Mitch Young
Greenhorn

Joined: Dec 15, 2006
Posts: 10
Thanks m coffee,
That makes sense, I'll do some testing with you solution.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19545
    
  16

First of all, even though a while loop is usually needed with ResultSets, in your case there will always be exactly one row (or an exception). That's why a simple call to rs.next() without while/if is enough.

Secondly, when you execute a statement for the second time, the ResultSets belonging to that statement are usually closed. That's exactly what was happening for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC ResultSet closing
 
Similar Threads
inserting an image in to database
can i use the resultset simultaneously?
Problem faced while closing the ResultSet..
Url cannot be null error :(
how to get the next sequence number ..