Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Java won't send SQL query

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zach Burnham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. See above
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!