Forums Register Login

Java won't send SQL query

+Pie Number of slices to send: Send
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.

Anyone got a thought?

Z
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

Z

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 ]
+Pie Number of slices to send: Send
Your error is a common oversight: in openConnection you define a local variable:

[B]Connection[B] con = DriverManager.getConnection((location + database), username, pass);

But at the end of the method, you return the null instance field:

return con; //a different con!
+Pie Number of slices to send: Send
 

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.

Make sense?
+Pie Number of slices to send: Send
Yes. See above
+Pie Number of slices to send: Send
Unrelated to your error:

1. Using exceptions to handle errors would be better that error code or boolean success/fail variables (loginSuccess).
2. You should close the connection, restult set and statement (using finally).
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1922 times.
Similar Threads
Help debug this program
About Pure java driver with JBOSS
Visibility help?
Please help debug this program
Cannot find symbol
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:58:46.