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 How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra" Watch "How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra" New topic
Author

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

Matthew Blake
Greenhorn

Joined: Aug 01, 2012
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

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

Joined: Oct 27, 2005
Posts: 19552
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Blake
Greenhorn

Joined: Aug 01, 2012
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

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

Joined: Aug 01, 2012
Posts: 10
Rob: any idea how to fix it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

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

Joined: Aug 01, 2012
Posts: 10
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

Joined: Aug 01, 2012
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19552
    
  16

You're welcome.
 
jQuery in Action, 2nd edition
 
subject: How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra
 
Similar Threads
Is there a MySQL server running on the machine/port you are trying to connect to?
Can get mysql DB connection from class, but can't get from servlet.
MySql connection issue
java jdbc
simple Sql connectivity problem