7

I have imported my library "mysql-connector-java-5.1.39" as answered by most people to exact same question , but I am still getting this error

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

here is the code of test class

package database;

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {

    public static void main(String[] args) throws Exception {
        getConnection();
    }

    public static Connection getConnection() throws Exception {
        try{
            String driver="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/test";
            String username="root";
            String password="root";
            Class.forName(driver);
            Connection conn= DriverManager.getConnection(url,username,password);
            System.out.println("connected");
            return conn;
        }
        catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
}

eclipse directory

Using mac OS .

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Faizan Ul Haq
  • 454
  • 2
  • 7
  • 23
  • Did you add the .jar file to build path? – ROMANIA_engineer May 16 '16 at 12:43
  • `Class.forName(driver);` for `com.mysql.jdbc.Driver` i don't see it imported in your Main class – Naman May 16 '16 at 12:43
  • Check your referenced libraries. Apparently your compiler doesn't see 'com.mysql.jdbc.Driver'. Also even if you have imported your jar, are you sure that this specific class is in that jar? – Dale May 16 '16 at 12:44
  • i added zip file to build bath > add external jars. i downloaded connector file from mysql official site it should have that class or not ? – Faizan Ul Haq May 16 '16 at 12:47

5 Answers5

5

Everybody repeat after me. ( :-) )

"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error.

Therefore, changing the >>build<< path or adding an import cannot fix the problem.

The solution is to make sure that the JAR file is on the classpath when you run the application. For example, if your test class is in bin\database\Main.class and the driver JAR is in lib ...

$ java -classpath bin:lib/mysql-connector-java-5.1.39.jar database.Main
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

If the jar is already added to your external libs, you can simply add :

import com.mysql.jdbc.Driver;

and it shall work in your class.

This shall help further SO-21580499

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353
  • You need to add the jar for the lib to your project as an external library. Then it shall work, the if clause in my answer. – Naman May 16 '16 at 12:52
  • Sir ! I added connector zip file to build bath > add external jars.which i downloaded from mysql official site – Faizan Ul Haq May 16 '16 at 12:55
  • 1
    I don't think that import statement is required. Class.forName uses reflection to get the class instance, and import is not required. Import is checked and resolved at the compile time. – deadpool May 16 '16 at 13:00
2

Some of the most possible reasons of "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" in your code are:

  1. You don't have mysql-connector.jar in your Classpath.
  2. mysql-connector.jar is in your classpath but somehow your classpath is getting overridden.
  3. mysql-connector.jar is in classpath but current user doesn't have read permission.
MeisterAjit
  • 108
  • 9
  • i have added connector zip from mysq site and added it through build path . it should be in class path or not ? – Faizan Ul Haq May 16 '16 at 12:53
  • 1
    Did you add zip file itself or its jar which is inside it ? You need to add jar not zip. It should be in your class path if you are running your program manually - Otherwise if you are using IDE then it will automatically from the lib directory where you have added it. – MeisterAjit May 16 '16 at 14:32
  • 2
    i was adding zip , now added jar file by placing it in class path and its done :) – Faizan Ul Haq May 16 '16 at 16:56
  • Nice. Good. All the best with your rest of the project. – MeisterAjit May 16 '16 at 18:49
  • 1
    The solution proposed by @MeisterAjit in the above comment is the only one that worked for me. I had added the ZIP instead of the JAR in Eclipse. An elementary bug that Eclipse needs to address. – Arvindh Mani Nov 07 '17 at 17:19
1

It looks like you app cannot find the JDBC driver for Microsoft SQL server. You can download it below and add the jar to your WEB-INF/lib:

https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774

Slava Imeshev
  • 1,360
  • 10
  • 14
1

Maybe you are missing any one of the following things : Both of the following things are necessary to remove the error :

  1. Adding mysql-connector.jar file to your WebContent / WEB-INF / lib folder.

  2. Adding mysql-connector.jar file in your classpath

Download mysql-connector.jar from https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.11

For solving first problem paste the above downloaded jar file into your project's WebContent / WEB-INF / lib folder.

For Solving second problem - right click on your project , go to properties then go to Java Build Path and then in Libraries section choose Add External JARs and select the above downloaded jar file.

Hope it helps.