Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

Using Java, I am getting this following error when trying 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 am using the mysql-connector-java-5.1.18-bin.jar driver. It is in my build path. I have also restarted MySQL. I have also logged on from the command line with root and no password and it has connected fine. I am not currently seeing a port 3306 in netstat. Earlier I was getting a different error (I didn't change the code). The error I was getting was 

"jdbc mysql Access denied for user 'root'@'localhost password NO"

Below is my code:

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();
}

1 Answer

0 votes
by (12.7k points)
edited by

In this specific case (assuming that the Class#forName() did not throw any exception; your code is namely proceeding with running instead of throwing the exception), this SQLException means that Driver#acceptsURL() has returned with false for any of the loaded drivers.

And certainly, your JDBC URL is wrong:

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

Remove the single quote:

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

Join the SQL Course by Intellipaat to Learn SQL concepts in detail and to get certified.

Browse Categories

...