This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes rs.next() returns true, but if (rs.next()) block not executed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "rs.next() returns true, but if (rs.next()) block not executed" Watch "rs.next() returns true, but if (rs.next()) block not executed" New topic
Author

rs.next() returns true, but if (rs.next()) block not executed

Woody Dvorak
Greenhorn

Joined: Sep 15, 2009
Posts: 3
In the following method:

At line #20... if (rs.next()) - the resultset contains a record, rs.next() returns true (I checked using a breakpoint), but the statements in the if block are not executed. The code jumps immediately to the finally block.

This is happening in several (new) methods I've written, but older methods containing similar logic / syntax are working fine.

What is wrong???

Thanks,
Woody
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29274
    
140

Woody,
Welcome to JavaRanch!

rs.next() always returns true on "select count(*)". The question is whether the count is greater than zero.

Can you try the following to see what the value of the count is?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Woody Dvorak
Greenhorn

Joined: Sep 15, 2009
Posts: 3
Hi, Jeanne... thanks for the response (and the welcome)!

I agree, my expectation is (as you pointed out) that I'll always get a RecordCount returned from this query, even if there are no matching records... and I am.

To reiterate, I put a breakpoint at line 20, examined the value of the expression rs.next()... it's true. But when I continue execution (step over), it doesn't execute the next statement in the if block... it jumps directly to the finally block. Seems impossible (!)... but that's what it's doing, consistently.

Using Eclipse JEE (Ganymede), app is deployed on JBoss 4.2.2, Oracle back end. No errors in JBoss log, no exceptions, no nothing... just weird. I've showed the behavior to a couple of other guys here, we're all scratching our heads. I've redeployed (many times), restarted JBoss, rebooted the server... no luck resolving this. Been fighting it for days...

BTW, this is a DAO class... and, as you can see, the connection is method-scoped. Also, I've copied the strSQL variable (while in break mode), pasted into Toad & executed... worked fine, returned a RecordCount of value 2 (or other value, depending on userSsn). I just can't get the code to continue executing the statements inside the if block...

Woody Dvorak
Greenhorn

Joined: Sep 15, 2009
Posts: 3
I put println statements in the if block and the catch block - not executing either of 'em. <sigh>

I'm playing around with Connection scoping, now... I wonder if using these similarly-named connections / statements / resultsets within multiple methods, within multiple DAO's might be introducing a conflict?
David Kilcy
Greenhorn

Joined: Aug 25, 2009
Posts: 20
try catching Throwable just as an experiment. Maybe you're getting a runtime exception thats not being caught.


On a side note, you should also put an independent try/catch around each "close()" statement. If one of your close statements throws an exception it will not finish the rest of your cleanup, and you will be leaking resources.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rs.next() returns true, but if (rs.next()) block not executed
 
Similar Threads
Optimal JDBC connection pooling configuration for tomcat server
rs.next() always return true even no result is querried
idiom for finally closing resources
Handle Huge Data
what is super?