0

I am a beginner in java and am trying to connect it with a database I am having this problem java.sql.SQLException

This is my code

import java.sql.*;

public class Driver {

public static void main(String[] args) {

    try {
        //1. Get a connection to database
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection myConn = DriverManager.getConnection("jdbc.mysql://localhost:3306/demo", "root"," ");

        //2.Create a statement

        Statement myStat = myConn.createStatement();
        //3.Execute SQL query

        ResultSet myRs = myStat.executeQuery("select * from employees");
        //4.Process the result set

        while(myRs.next())
        {
            System.out.println(myRs.getString("last_name")+ ","+myRs.getString("first_name"));
        }

    }catch (Exception exc) {
        exc.printStackTrace();
    }

}

} and this is the error

java.sql.SQLException: No suitable driver found for 
jdbc.mysql://localhost:3306/demo
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at jdbcdemo.Driver.main(Driver.java:12)

I had already applied the mysql-connector-java version.jar in the library but it's not working .can someone help me?

Afif
  • 1
  • 3

2 Answers2

-1

You are getting No suitable driver found error because MySQL-connector is not properly added as a dependency. If you are using Eclipse as an IDE try to add external library from project build path. An alternative way is clean-refresh your project and verifies dependencies are defined in the classpath or not.

-1

1

Add the jar file into WEB-INF/lib/ folder

2

Right click on the project folder Select Build path Select Configure build path Select Libraries Select Add external JARS Add the jar

3

DriverManager.getConnection("jdbc.mysql://localhost:3306/demo", "root"," ")

In this statement replace "demo" with a existing database name which you created and make sure your mysql server is up and running

Community
  • 1
  • 1
Sanath Kumar
  • 163
  • 1
  • 12