This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
According to the spec, ResultSet.TYPE_FORWARD_ONLY means that you can only move the cursor forward through a result set.
In the code below, I create a statement and specify ResultSet.TYPE_FORWARD_ONLY. After executing the query, I position the cursor at the end of the ResultSet using afterLast() and then call previous() to try to move backwards. I'd expect the code to fail at this point as I've specified ResultSet.TYPE_FORWARD_ONLY.
However, the code runs successfully and prints the last row. Why didn't it fail?
Not sure if it's relevant, but I'm connecting to a local MySQL DB and running the script via Eclipse.
So... Oracle's driver implements the API correctly (the API says that calling afterLast on a forward-only ResultSet should throw an SQLException) and MySQL's doesn't. You might well consider that to be a bug on MySQL's part.
Joined: Sep 18, 2013
Thanks for your input guys. I'd assumed that I'd somehow misunderstood the spec.
When you're learning new stuff, it's natural to assume that you've got the wrong end of the stick rather than that the software itself is misbehaving.
Joined: Aug 19, 2013
No worries. I'd have reacted in the same way; it's maddening when you find inconsistencies or contradictions!
Have you tried running it using the command line, btw? It shouldn't make any difference (I suppose; I have zero experience with Eclipse) but it would be good just to exclude the Eclipse side of things.
Frank Rennie wrote:When you're learning new stuff, it's natural to assume that you've got the wrong end of the stick rather than that the software itself is misbehaving.
That's right. And even when you're an experienced programmer you should still assume that it's your fault, rather than somebody else's fault, until proved otherwise. That's just a more practical way of solving problems.
JDBC drivers are wild beasts. I've once hunted for a bug in a jTDS driver (yes, there is one too ) and looked at the source code. There were some comments which expressed the authors' uncertainty about what some particular method is exactly supposed to do.
The bug I've mentioned has taught me that it is not advisable to test an application using a JDBC driver/database different from the one the system will be run on. The JDBC drivers aren't born equal.