The java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb exception occurs if the suitable driver is not found to connect mysql database from java application. The MySQL JDBC driver is not loaded in java either because the driver jar is not available in the class path, or because it is not possible to load the mysql driver jar. If no suitable driver is found in the java class path, the exception java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb will be thrown.

The MySQL JDBC driver is used to connect your Java application to a MySQL database. The mysql driver sends the database query from java to the database. The Mysql database executes the query and returns the results. The MySQL JDBC driver receives the data from the MySQL database and sends it back to the Java application.

If the MySQL JDBC driver is not loaded, the Java program will throw the exception java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb.



Exception

The stack trace of the exception will be shown as shown below. The exception java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb  is due to the driver class not loaded in java.

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb
	at java.sql.DriverManager.getConnection(DriverManager.java:689)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.yawintutor.DBConnection.main(DBConnection.java:13)


How to reproduce this exception

If the Java application can not load the MySQL JDBC driver class or the MySQL JDBC class is not available in the Java class path, the exception java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb will be thrown. This exception will be reproduced in the example below.

package com.yawintutor;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
	public static void main(String[] args) throws Exception {
		String url = "jdbc:mysql://localhost:3306/testdb";
		String username = "root";
		String password = "root";

		Connection con = DriverManager.getConnection(url, username, password);
		if (con != null) {
			System.out.println("Database Connected successfully");
		} else {
			System.out.println("Database Connection failed");
		}
	}
}


Solution 1

If the MySQL JDBC driver jar is available in the class path and the driver class is unable to load, the driver class must be loaded in the java using class.forName() method. The forName() method will load the class from the fully qualified class name specified as an argument. The example below demonstrates how to load the MySQL JDBC driver class using the class.forName() method.

package com.yawintutor;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/testdb";
		String username = "root";
		String password = "root";

		Connection con = DriverManager.getConnection(url, username, password);
		if (con != null) {
			System.out.println("Database Connected successfully");
		} else {
			System.out.println("Database Connection failed");
		}
	}
}


Solution 2

If you are using mysql database version till 5.x.x, the MySQL JDBC driver “com.mysql.cj.jdbc.Driver” will not be available. For the older mysql databases the driver class “com.mysql.jdbc.Driver” must be used. The java program will be as like below.

package com.yawintutor;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/testdb";
		String username = "root";
		String password = "root@123";

		Connection con = DriverManager.getConnection(url, username, password);
		if (con != null) {
			System.out.println("Database Connected successfully");
		} else {
			System.out.println("Database Connection failed");
		}
	}
}


Solution 3

If the MySQL JDBC driver jar is not available in the java class path, download the mysql driver jar from https://dev.mysql.com/downloads/connector/j/. The name of mysql driver jar is same as mysql-connector-java-8.0.20.jar. Add the jar to the java class path.

download the jar mysql-connector-java-8.0.20.jar from https://dev.mysql.com/downloads/connector/j/ 
( 
  goto https://dev.mysql.com/downloads/connector/j/
  select "Select Operating System:" as your operating system and install
      or
  select "Select Operating System:" as "Platform Independent", download the zip and extract
)

Add in your java project.

In eclipse
Right click in mysql-connector-java-8.0.20.jar -> Build Path -> Add to Build Path


Solution 4

If the java project is a maven project, add the mysql connector dependency in the pom.xml. The dependency will download the mysql-connector-java-8.0.22.jar in the repository and link to the project. The latest mysql connector dependency can be found in https://mvnrepository.com/artifact/mysql/mysql-connector-java

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.22</version>
</dependency>


Solution 5

If the java project is build using the Gradle, add the dependency as below. When the project is build, the MySQL JDBC driver will be added to the java project.

dependencies {
    compile 'mysql:mysql-connector-java:8.0.22'
}



Leave a Reply