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.
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.
Joined: Dec 15, 2006
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.
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.