hey guys, here is the deal: I am a student, who is tryin to learn java. If there is a good tutorial some where,just post a link and I will be glad to RTFM. I am trying to connect to a mySql database from tomcat, but get the following error message: The code for the connection class is here:
I believe your problem code is here: What your code is doing is: if connection is null then perform "statement = connection.createStatement();" in which connection is null. You do create a new Connection called "conn" if connection is null, but never use it ( you use the old null connection ) Jamie
Jeremiah Elliott
Greenhorn
Joined: Nov 01, 2001
Posts: 17
posted
0
Thanks for the tip, however it has not solved the problem. this is what I have done:
the error is on line 51, based on the tomcat error message. line 51 is: return statement.executeQuery(sql); I also tried this code where you where saying the error was:
but it had the same results. thanks for your time, Jeremiah
Jeremiah: This line of code worries me. Maybe someone can verify my thoughts.
If I am right, both conditions get evaluated every time. If this is the case, the order of events would be 1. connection == null evaluates to true 2. It tries to evaluate connection.isClosed() but since it is null, it throws a null pointer exception. maybe you can rework your code like this:
This would ensure that your connection is not null before you call the method isClosed(). let me know if this is the case Jamie
Dave Wingate
Ranch Hand
Joined: Mar 26, 2002
Posts: 262
posted
0
Actually, imho, the line you mention uses the logical short-circuit operator and, as such, should never try to invoke the method of connection if the value of connection is null: if (connection == null || connection.isClosed()){ the short-circuit "or" essentially says "if the first statement is true, just stop looking and return true." So, if it is the case that the value of connection is null, evaluation of the boolean immediately stops and returns true. had the statement been: if (connection == null | connection.isClosed()) I think your concern would be valid.