File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java won't send SQL query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java won Watch "Java won New topic
Author

Java won't send SQL query

Zach Burnham
Greenhorn

Joined: Feb 15, 2006
Posts: 16
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Zach Burnham
Greenhorn

Joined: Feb 15, 2006
Posts: 16
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 ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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!


There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?
Zach Burnham
Greenhorn

Joined: Feb 15, 2006
Posts: 16
Yes. See above
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java won't send SQL query