3

I am trying to use a Java class that already worked great for me in the past in order to access MySQL from my JSP project, running Tomcat 6. The problem is, that this line:

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "password");

gives con a value of null.

Now, my question is - what can cause the "getConnection" method return null?

Edit: I see now that also, I get the infamous ClassNotFoundException: com.mysql.jdbc.Driver. I have the connector included in my build path. So why am I getting this error? And, does it have to do with getConnection returning null?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
uvero
  • 81
  • 1
  • 1
  • 9

4 Answers4

0

You need to initialize the driver before you can get a connection from the DriverManager.

Class.forName("com.mysql.jdbc.Driver"); 
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
0

Your url is incomplete. The jdbc:mysql://localhost:3306 should be followed by the database name.

Trouble shoot steps:

  1. Check the port
  2. Check if the db server is turned on
Zeus
  • 6,386
  • 6
  • 54
  • 89
  • the database name is not mandatory. if you want to use connections with different database schemes, you omit the name and place it into the query: 'SELECT * FROM .'
    – benez Aug 10 '17 at 10:06
0

According to this article(which has a good example) http://www.vogella.com/tutorials/MySQLJava/article.html,

"con" will be null if the getConnection throws an exception and is caught in your code.

Like:

Connection con = null;
 try{ 
con = DriveManager.getConnection("Not right url");   

}catch(SQLException sqlE){};
System.out.println(con); //it will be null

you need to do

   connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"
          + "user=sqluser&password=sqluserpw");

So maybe have "user = root&password = password"

Also if there is ClassNotFoundException most likely you need a jar file

DriverManager API: http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)

Rika
  • 768
  • 1
  • 5
  • 16
  • (if there is something wrong with answer please put why thank you) – Rika Sep 24 '14 at 19:50
  • I didn't downvote, but your link is irrelevant to this question (and if it was you should at least have quoted or summarized the relevant parts) and `DriverManager.getConnection` by specification never returns `null`. You can verify this by looking at the `DriverManager` code. – Mark Rotteveel Sep 25 '14 at 07:53
  • @MarkRotteveel In my answer I said if getConnection is not done properly it is going to return null because most likely of a connection error, it looks like his string is not appropriate as the parameter is not done right, but it also could be he doesn't have a jar file, it is pretty much like the link you provided to him – Rika Sep 25 '14 at 13:27
  • 1
    But `DriverManager.getConnection()` is **never** going to return `null`. It either returns a `Connection` instance, or it throws an `SQLException`. – Mark Rotteveel Sep 25 '14 at 14:00
0

If you are working with JDBC 3 then you need to load the driver like

Class.forName("com.mysql.jdbc.Driver"); 

If you are working with JDBC 4 then you are no longer required to manually load the JdbcDriver.

And then pass valid connection_url to getConnection

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDbName", "root", "password");

Lastly make sure that mysql driver jar is in classpath.

sol4me
  • 15,233
  • 5
  • 34
  • 34