This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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:
 
Marshal
Posts: 25795
69
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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Marshal
Posts: 25795
69
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
Marshal
Posts: 25795
69
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
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Hold that thought. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic