aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 29233
    
136

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
idiom for finally closing resources
rs.next() always return true even no result is querried
Handle Huge Data
Optimal JDBC connection pooling configuration for tomcat server
what is super?