1

I want to connect to my DB, but always get an exeption: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've installed Connector J as a library on NetBeans: enter image description here

Here is the code in main:

import java.sql.SQLException;

public class Mavenproject2 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        
        try{
             Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
             System.out.println("Connection succesfull!");
         }
         catch(Exception ex){
             System.out.println("Connection failed...");
              
             System.out.println(ex);
         }
        
        //DBHandler dbh = new DBHandler();
        //dbh.signUser("Andrei", "Ker", "test", "123456789");
    }
}

I always get "Connection failed...".

I deleted Class.forName as I was advised and lounched the main code, but got the new exeption: Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test_schema

But I could connect to this DB in NetBeans and see all its columns and rows. So if driver is preinstaled, what cause the exeption? enter image description here

import java.sql.Connection;
import java.sql.DriverAction;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBHandler extends Configs {

    Connection dbCon;

    public Connection getDbConnection() throws ClassNotFoundException, SQLException {
        String connectionString = "jdbc:mysql://" + dbHost + ":"
                + Port + "/" + Name;
        //Class.forName("com.mysql.cj.jdbc.Driver");

        dbCon = DriverManager.getConnection(connectionString, User, Pass);
        return dbCon;
    }

    public void signUser(String name, String surename, String login, String pass) throws SQLException, ClassNotFoundException {
        String insert = "INSERT INTO " + Const.USER_TABLE + "(" + Const.USER_FIRSTNAME+")"+"VALUES(?)";
        PreparedStatement ps = getDbConnection().prepareStatement(insert);
        ps.setString(1, name);
        ps.executeUpdate();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I'd split this problem up in multiple steps. `Class.forName("com.mysql.jdbc.Driver")` alone should tell you if the driver is available. Maybe there's just some problem with instancing? And as far as I can remember, I never needed to instantiate drivers myself. So get rid of `.getDeclaredConstructor().newInstance();` and try to go from there. – JayC667 Jul 17 '22 at 09:29
  • Just creating an instance of class `com.mysql.jdbc.Driver` does not open a connection to the database. In fact, that whole line of loading the driver manually and creating an instance should not be necessary (unless you are using a very old JDBC driver). The error message means that you do not have a class with that name on the classpath when you run your program. – Jesper Jul 17 '22 at 09:39
  • What is the version of your MySQL DB? – Harry Coder Jul 17 '22 at 10:21
  • The version is 8.0.29 – Andrei Kernazhytski Jul 17 '22 at 10:31
  • The driver is available for NetBeans itself, that does not mean it is automatically available for your project. You need to explicitly add the MySQL Connector/J library to the build path/classpath/dependencies of your NetBeans project. – Mark Rotteveel Jul 17 '22 at 10:53

0 Answers0