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

MySQLMySQLi Database

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");

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

The snapshot of code is as follows −

The following is the output −

Connection is successful !!!!!

The snapshot of the sample code is as follows −

raja
Published on 26-Feb-2019 11:01:37
Advertisements