0

I downloaded the latest version of mysql connector "mysql-connector-java-8.0.11" and I tried to connect it with java using netbeans but it won't work also, I added the jar file in my project lib but nothing happened it gives me this error:

 Exception in thread "main" java.lang.ClassNotFoundException: 
 com.mysql.jdbc.cj.Driver
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:264)
 at learnjdbc.Learnjdbc.main(Learnjdbc.java:18)

this is my code

import java.sql.*;
public class Learnjdbc {

public static String name="root";
public static String password="ismail19972018";
public static String url="jdbc:mysql://localhost/myinfo";
public static void main(String[] args) throws ClassNotFoundException {
    Connection connect=null;
    Statement stm=null;
    PreparedStatement prstm=null;
    ResultSet rs=null;


    try{
        Class.forName("com.mysql.jdbc.cj.Driver");
        connect=DriverManager.getConnection(url,name,password);
        System.out.println("connected");

    }catch(SQLException ex){



           ex.printStackTrace();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Ismail Bouaddi
  • 165
  • 1
  • 9
  • [tag:mysql-error-1064] has exactly nothing to do with it, or any other MySQL error code. MySQL hasn't even executed yet. Don't tag indiscriminately. – user207421 May 20 '18 at 02:24
  • Either you don't have the MySQL Connector/J driver on the classpath, or you still have MySQL Connector/J 5.1.x or earlier (which doesn't have `com.mysql.jdbc.cj.Driver`, but `com.mysql.jdbc.Driver`). – Mark Rotteveel May 20 '18 at 10:52

2 Answers2

1

The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.cj.jdbc.Driver. The class name in the code com.mysql.jdbc.cj.Driver does not exists which causes the ClassNotFoundException.

pulkit-singhal
  • 845
  • 5
  • 15
0

cannot connect

This is not 'cannot connect', it is 'cannot find class', and the Class.forName() line has been unnecessary since 2007.

Just remove it.

user207421
  • 305,947
  • 44
  • 307
  • 483