0

I am getting this Error - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. I am using Notepad++ to code and command prompt to execute this program. No IDE.

I have installed latest version of MySQL,Java SE,Java Connector. I have set CLASSPATH variable correctly to mysql-connector-java-8.0.20.jar file. You can check CLASSPATH in below image [1]: https://i.stack.imgur.com/9XgQp.jpg

My Code is also Correct I have placed my code and output below. //Database program for MYSQL with java. Simple connection in notepad.

import java.sql.*;
public class Db01
{
    public static void main(String args[])
    {
        try {
        System.out.println("Start of program");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root"); 
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("select * from studenttable");
        while(rs.next())  
        System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
        con.close();  
        }catch(Exception E)
        {
            System.out.println(E);
            System.out.println("Program end with cought exception");
        }
    }
}

and Heres my output. Start of program

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Program end with cought exception

I tried most Documentations and forums but could not find answer please help me about this.

Yogiraj
  • 41
  • 6
  • I am not sure what are you ding with Class.forName("com.mysql.jdbc.Driver"); If you remove this line It is not working? – vmrvictor Jul 07 '20 at 14:20
  • 1
    That statement is used to register drivers. Its optional statement but am using it. Heres what happens when I dont use that statement - - java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/student – Yogiraj Jul 07 '20 at 14:43
  • so you can just add it to your classpath, download the mysql connector https://dev.mysql.com/downloads/connector/j/ and then add the JAR to your dependencies, if using intellij then go to right click "Module settings/Dependencies" and add your downloaded connector – vmrvictor Jul 13 '20 at 10:51

1 Answers1

-1

I found the Answer of this. This is problem of Incompatible driver. I have read documentations from official site of MYSQL. It Explains this. There are two series of JDBC Pure Java Drivers by MYSQL.

1.Connector J 8. Series - This drive driver adds many extra facilities, but most of the times on small basic programs it cant work on its own. It needs core driver 5.0 installed.

2.Connector J 5. Series - This looked like most compatible driver series. Its original driver you need to install. So if check if you are using correct driver.

Your 5.0 Driver will be present in C:\Program Files (x86)\MySQL\Connector J 5.1\mysql-connector-java-5.1.48.jar by default. Create CLASSPATH Variable, give location to this driver and your .class file of program. Now it will work.

EDITS : I previously said J7 Driver is Add-on to J5 driver. I was wrong, Someone corrected me in comments. So I have reedited this post. I cant guarantee my above described answer true, cause am learning myself. I just tried to tell how I got my solution.

Yogiraj
  • 41
  • 6
  • This can't be a problem of 'incompatible' drivers. The MySQL Connector.J 8.0.x versions still contain `com.mysql.jdbc.Driver`, although it has been deprecated. And it is not an 'add on to the existing core driver' (whatever that means). The error would indicate that you just didn't have the driver on your classpath. Also be aware that most ways of running Java applications do not use the `CLASSPATH` environment variable, so it is a good habit to unlearn that and use the more accepted ways of specifying the classpath. – Mark Rotteveel Jul 07 '20 at 16:40
  • Furthermore, the Connector/J 5 series is not the 'most compatible' driver. It is recommend to use MySQL Connector/J 8 unless you have a very good reason not to. Version 8 should work in all the situations that version 5 should work (though it might not support very old versions of MySQL). – Mark Rotteveel Jul 07 '20 at 16:41