Resolve java.sql.SQLException: No suitable driver found for localhost test?


You will get this type of exception whenever your JDBC URL is not accepted by any of the loaded JDBC drivers by the method acceptsURL. You need to mention the MySQL JDBC driver which is as follows −

The MySQL JDBC url is as follows −

jdbc:mysql://localhost:3306/test?useSSL=false

The prototype of acceptsURL is as follows −

boolean acceptsURL(String url) throws SQLException

The acceptsURL returns boolean that means if the JDBC driver understands the database URL it returns true otherwise false. It takes one parameter of type String which is a database URL.

The entire database URL connection is as follows. The syntax −

con = DriverManager.
getConnection("jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false", "yourUserName", " yourPassword");

Example

The Java code is as follows −

import java.sql.Connection;
import java.sql.DriverManager;
public class AvoidSQLException {
   public static void main(String[]args){
      Connection con = null;
      try {
         con = DriverManager.
         getConnection("jdbc:mysql://localhost:3306/sample?useSSL=false", "root", "123456");
         System.out.println("Connection is successful !!!!!");
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Output

The snapshot of code is as follows −

The following is the output −

Connection is successful !!!!!

The snapshot of the sample code is as follows −

Updated on: 30-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements