How to solve java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test

The error "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" occurs when you try to connect MySQL database running on your localhost, listening on port 3306 port from Java program but either you don't have MySQL JDBC driver in your classpath or driver is not registered before calling the getConnection() method. Since JDBC API is part of JDK itself, when you write a Java program to connect any database like MySQL, SQL Server or Oracle, everything compiles fine, as you only use classes from JDK but at runtime, when the JDBC driver which is required to connect to database is not available, JDBC API either throws this error or "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver".

Most common reason of this error is missing MySQL JDBC Driver JAR e.g. mysql-connector-java-5.0.8.jar not available in classpath. Another common reason is you are not registering the driver before calling the  getConnection() and you are running on Java version lower than 6 and not using a JDBC 4.0 compliant driver. We'll see these reasons in more detail in this article.


JAR not available in Classpath

If mysql-connector-java-5.0.8.jar is not available in classpath then you cannot connect to MySQL database from Java. Your program like below will compile fine but as soon as you will run it you will get the error "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" because of the JDBC URL format "jdbc:mysql" is not matching with any registered JDBC driver.


Here is our Java program to demonstrate this error. This program reproduce this error by first leaving out the required JDBC JAR from classpath and also not explicitly registering the driver before use by not calling Class.forName() method.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/*
 * Java Program to to connect to MySQL database and
 * fix java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306
 * error which occur if JAR is missing or you fail to register driver.
 */

public class Main {

  public static void main(String[] args) {

    Connection dbConnection = null;

    try {
      String url = "jdbc:mysql://localhost:3306/test";
      Properties info = new Properties();
      info.put("user", "root");
      info.put("password", "test");

      dbConnection = DriverManager.getConnection(url, info);

      if (dbConnection != null) {
        System.out.println("Successfully connected to MySQL database test");
      }

    } catch (SQLException ex) {
      System.out.println("An error occurred while connecting MySQL databse");
      ex.printStackTrace();
    }

  }

}

Output
An error occurred while connecting MySQL databse
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:187) 
at Main.main(Main.java:24))

How to solve java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/



You need to do two things in order to solve this problem:

1) Add mysql-connector-java-5.0.8.jar or any other MySQL JAR corresponding to the MySQL database you are connecting. If you don't have MySQL JDBC driver, you can download from here http://dev.mysql.com/downloads/connector/j/3.1.html

2) Add following line of code just before the call to Connection.getConnection(url, props) method

// load and register JDBC driver for MySQL
Class.forName("com.mysql.jdbc.Driver"); 

This will load the class, the JDBC driver to connect MySQL, com.mysql.jdbc.Driver from mysql-connector-java-5.0.8.jar and register it with JDBC API. Once you do that "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" will go away.


Also, it's worth noting that JDBC 4.0 released with Java SE 6 has now introduced auto-loading of JDBC driver class, which means you don't need Class.forName("com.mysql.jdbc.Driver"); any more, but only when you are running on at least Java 6 and your driver JAR is also JDBC 4.0 compliant

For example, the driver used in this program "mysql-connector-java-5.0.8.jar" is not JDBC 4.0 compliant, so even if you run this program in Java 6, 7 or Java 8, it will not work, but if you use mysql-connector-java-5.1.36.jar then even without adding "Class.forName("com.mysql.jdbc.Driver");", your program will work fine. Why? because JDBC will automatically load and register the driver, provided you have mysql-connector-java-5.1.36.jar file in your classpath. . See Core Java Volume 2 - Advanced features to learn more about new features introduces in JDBC 3.0 and JDBC 4.0 releases.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test [Solution]


That's all about how to solve "java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test" error in Java program. This error occurs if JDBC is not able to find a suitable driver for the URL format passed to getConnection() method e.g. "jdbc:mysql://" in our case. In order to solve this error, you need the MySQL JDBC driver e.g. mysql-connector-java-5.1.36.jar in your classpath. If you use a driver which is not JDBC 4.0 compliant then you also need to call the Class.forName("com.mysql.jdbc.Driver") method to load and register the driver.


Other JDBC troubleshooting guides for Java Programmers
  • How to connect to MySQL database from Java Program [steps]
  • How to connect Microsoft SQL Server from Java Program [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to connect Oracle database from Java and Eclipse? [guide]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • SQLServerException: The index 58 is out of range - JDBC [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [solution]
  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [Solution]


References


No comments :

Post a Comment