59

Using Java, I get this error when attempting to connect to a mysql database:

java.sql.SQLException: No suitable driver found for 
jdbc:mysql://localhost:3306/mysql at
java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MyTest1.main(MyTest1.java:28)

I'm using the mysql-connector-java-5.1.18-bin.jar driver. It is in my build path. I have restarted MySQL. I've also logged on from the command line with root and no password and it connected fine. I'm not currently seeing a port 3306 in netstat. Previously I was getting a different error (I didn't change the code). The error was "jdbc mysql Access denied for user 'root'@'localhost password NO"

try {
    Class.forName("com.mysql.jdbc.Driver");
} 
catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 


try {
    String url = "jdbc:mysql://localhost:3306/mysql";
    Connection con = DriverManager.getConnection(url, "root", "");
}
catch (Exception e){
    e.printStackTrace();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user994165
  • 9,146
  • 30
  • 98
  • 165
  • Can you post the mysql command line that you are using? It may be just a case where one is using "localhost" and the other 127.0.0.1. – hafichuk Nov 16 '11 at 04:41
  • few points in this link on not importing anything ... http://dev.mysql.com/doc/refman/5.1/en/connector-j-usagenotes-basic.html#connector-j-usagenotes-connect-drivermanager – r0ast3d Nov 16 '11 at 04:42
  • 1
    It doesn't matter if it's on your build path when you're running it--is it on your run-time classpath? – Dave Newton Nov 16 '11 at 05:11
  • Are you sure you have mysql-connector-java-5.1.18-bin.jar on classpath? – Igor Nikolaev Nov 16 '11 at 05:12
  • 1
    I'm using Eclipse so when I added the library, it should have added it to the classpath. The jar is under refernced libraries. I updated the command above. The error is on the "Connection" line – user994165 Nov 16 '11 at 06:10
  • Related question: http://stackoverflow.com/q/5556664/632951 – Pacerier Jul 29 '16 at 10:25

8 Answers8

50

In this particular case (assuming that the Class#forName() didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException means that Driver#acceptsURL() has returned false for any of the loaded drivers.

And indeed, your JDBC URL is wrong:

String url = "'jdbc:mysql://localhost:3306/mysql";

Remove the singlequote:

String url = "jdbc:mysql://localhost:3306/mysql";

See also:

krock
  • 28,904
  • 13
  • 79
  • 85
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • For me the error didn't go, using ``url = "jdbc:mysql://localhost:3306/dbname?useSSL=false";`` worked for me. – Fawad Dec 16 '20 at 03:39
26

You have to set classpath for mysql-connector.jar

In eclipse, use the build path

If you are developing any web app, you have to put mysql-connector to the lib folder of WEB-INF Directory of your web-app

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
prashant kumar
  • 261
  • 3
  • 3
10

When using Netbean, go under project tab and click the dropdown button there to select Libraries folder. Right Click on d Library folder and select 'Add JAR/Folder'. Locate the mysql-connectore-java.*.jar file where u have it on ur system. This worked for me and I hope it does for u too. Revert if u encounter any problem

5

A typographical error in the string describing the database driver can also produce the error.

A string specified as:

"jdbc:mysql//localhost:3307/dbname,"usrname","password"

can result in a "no suitable driver found" error. The colon following "mysql" is missing in this example.

The correct driver string would be:

jdbc:mysql://localhost:3307/dbname,"usrname","password"
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
Karthick
  • 51
  • 1
  • 2
  • Great! I had the exact same symptoms as the OP, and you guessed exactly the mistake I made: after adding port number at the wrong place **jdbc:mysql:3306//localhost** I deleted it together with the column: **jdbc:mysql//localhost** and became extremely puzzled, where did my `com.mysql.jdbc.Driver` go? – MKaama Oct 05 '14 at 09:50
  • Thanks so much! I was missing a ':' in my jdbc string and sure enough ran into this issue. :-) – Joseph Bleau Nov 10 '15 at 18:51
4

This error happened to me, generally it'll be a problem due to not including the mysql-connector.jar in your eclipse project (or your IDE).

In my case, it was because of a problem on the OS.

I was editing a table in phpmyadmin, and mysql hung, I restarted Ubuntu. I cleaned the project without being successful. This morning, when I've tried the web server, it work perfectly the first time.

At the first reboot, the OS recognized that there was a problem, and after the second one, it was fixed. I hope this will save some time to somebody that "could" have this problem!

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
1

i had same problem i fix this using if developing jsp, put mysql connetor into WEB-INF->lib folder after puting that in eclipse right click and go build-path -> configure build patha in library tab add external jar file give location where lib folder is

hunter
  • 11
  • 1
0

Just telling my resolution: in my case, the libraries and projects weren't being added automatically to the classpath (i don't know why), even clicking at the "add to build path" option. So I went on run -> run configurations -> classpath and added everything I needed through there.

Mr Guliarte
  • 739
  • 1
  • 10
  • 27
0

( If your url is correct and still get that error messege ) Do following steps to setup the Classpath in netbeans,

  1. Create a new folder in your project workspace and add the downloaded .jar file(eg:- mysql-connector-java-5.1.35-bin.jar )
  2. Right click your project > properties > Libraries > ADD jar/Folder Select the jar file in that folder you just make. And click OK.

Now you will see that .jar file will be included under the libraries. Now you will not need to use the line, Class.forName("com.mysql.jdbc.Driver"); also.

If above method did not work, check the mysql-connector version (eg:- 5.1.35) and try a newer or a suitable version for you.

Malith
  • 381
  • 6
  • 18