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