Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra

 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am finishing a program and I am having trouble getting the database to connect properly. I just completely removed and reinstalled MySQL so I don't think the error is caused by a bad password. Thanks for your help!

The error I'm getting is:

Driver loaded, but cannot connect to db: jdbc:mysql://localhost:3306/registrar
Exception in thread "main" java.lang.NullPointerException
at student.Undergraduate.add(Undergraduate.java:156)
at student.StudentAppDriver.addStudent(StudentAppDriver.java:102)
at student.StudentAppDriver.start(StudentAppDriver.java:30)
at student.StudentAppDriver.main(StudentAppDriver.java:20)

Code:
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Blake wrote:Driver loaded, but cannot connect to db: jdbc:mysql://localhost:3306/registrar
Exception in thread "main" java.lang.NullPointerException
at student.Undergraduate.add(Undergraduate.java:156)
at student.StudentAppDriver.addStudent(StudentAppDriver.java:102)
at student.StudentAppDriver.start(StudentAppDriver.java:30)
at student.StudentAppDriver.main(StudentAppDriver.java:20)


That's a stack trace. One of its purposes is to tell you exactly where the exception was thrown. In this case, starting from the top, it was thrown at line 156 of the "student.Undergraduate" class, which is inside a method called "add". And that line of code was called from line 102 of the "student.StudentAppDriver", which is inside a method called "addStudent".

You posted some other code, but that's just because your guess about the source of the problem wasn't correct. So now that you have something pointing to the actual problem, you should start by looking at that code instead. As always, if you have more questions, we're always here.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NPE probably stems from result never being initialized, as that step fails.

Matthew, can you add e.printStackTrace() after line 36 and print the results? Because you're getting an SQLException, but you ignore the exception itself. You shouldn't, as it contains the reason why the SQLException occurs.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul: I took a look at the lines mentioned but didn't see anything wrong with the code. Netbeans is not throwing any errors at me either.

Rob: I added e.printStackTrace() after line 36, but there is nothing different in the output box. Should I be looking somewhere else for the information?

Additional code from class StudentAppDriver:
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The NPE probably stems from result never being initialized, as that step fails.


Ah, I get it now... the error message comes from the posted code, and the stack trace comes from something which calls it but fails because it doesn't return a Connection properly.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: any idea how to fix it?
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a design problem with your getSimpleConnection() method. It should always return a Connection object; returning null, as it is now, is a bad thing. So in particular it should either return a valid Connection or it should throw an exception explaining why not. Ignoring the error, as you basically do now, is not a good idea.

And as Rob says, when you do catch an exception, you shouldn't ignore it and display some generic method. The exception tells you what went wrong, so printing its stack trace is a good idea.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for sticking with me, I struggle with programming, but hopefully I can get my head around this.

I made a couple changes. The first is I added a connection just under getSimpleConnection() with: Connection conn = null
At the end, just above the first try/catch I placed: conn = DriverManager.getConnection(DB_CONN_STRING, USER_NAME, PASSWORD);
I also have e.printStackTrace() at line 38. Did I implement it correctly?
Lastly, I removed result completely and replaced it with conn throughout the code
This seems to have no effect though. I must be missing some a concept somewhere.
Here is my current code with the updates I made:
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got this working. It appeared to be a few different problems ranging from proper login credentials to needing to revise my database table. Thanks for your help gentleman!
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic