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.
I'm trying to have Java read some basic information from a MySQL database and echo it. I've searched online and as far as I can tell my code matches what I've found. However, the result is a null returnSet.
A couple of classes here:
The output looks like this:
The output from the mySQL console:
From the mySQL log:
I don't get it. The query is good, and returns results on the console, but it looks like Java is never even sending the query; it sets some default values but never sends the query.
Maybe the "null" is the result of printing g.getMessage() in the last handler; exceptions do, indeed, have null messages sometimes, especially system ones like NullPointerException and ClassCastException. You've got a couple of event handlers that neglect to call ex.printStackTrace() -- change them so that they do, and I bet the results will give you a very strong hint about what to do next.
Ok so I changed the last handler (in mySQLHandlerTest) to look like this:
and now I get:
Which leads me to conclude that 'con' is never made or the connection object isn't passed back to mySQLHandlerTest properly. I'd suspect the latter, since doesn't throw the exception. It looks like the Connection object 'con' isn't being passed back properly, but I'm not sure how to check.
Edit: Whoops, looks like I never set up the boolean loginSuccess in mySQLHandler.java . Working on it now.
[ March 01, 2006: Message edited by: Zach Burnham ]
Edit 2: Uh. Well when you instantiate an object twice it gets grouchy. Especially when the method tries to return that object when it's out of its scope. So I edited line 36 of mySQLHandler.java to read
con = DriverManager.getConnection((location + database), username, pass);
instead of Connection con = etc
Hopefully someone will learn from my dunderhead newbie mistake [ March 01, 2006: Message edited by: Zach Burnham ]
Originally posted by Zach Burnham: It looks like the Connection object 'con' isn't being passed back properly, but I'm not sure how to check.
Now that you mention it, I see one classic mistake that everyone makes once, but only once: there are two "Connection con" variables in the mysqlHandler class. One is a member variable, and one is a local in openConnection. The local is declared inside the try block, which means its scope is limited to the try block. The one you return from the routine is the member -- which is always null.
To fix this, turn the "con" inside that try block from a declaration into a simple assignment -- remove the word "Connection", so that "con" there refers to the member, not a new local variable.