java.lang.ClassNotFoundException: com.mysql.jdbc.Driver MySQLConnector/J
Asked Answered
R

22

116

What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database MySQL.

Here is my code

package com.glomindz.mercuri.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySingleTon {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";

    private static MySingleTon myObj;   
    private Connection Con ;
    private MySingleTon() {
        System.out.println("Hello");
        Con= createConnection();
    }
    
    @SuppressWarnings("rawtypes")
    public Connection createConnection() {
        Connection connection = null;
        try {
            // Load the JDBC driver
            Class driver_class = Class.forName(driver);
            Driver driver = (Driver) driver_class.newInstance();
            DriverManager.registerDriver(driver);
            connection = DriverManager.getConnection(url + dbName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    /**
     * Create a static method to get instance.
     */
    public static MySingleTon getInstance() {
        if (myObj == null) {
            myObj = new MySingleTon();
        }
        return myObj;
    }
    
    public static void main(String a[]) {
        MySingleTon st = MySingleTon.getInstance();
    }
}

I am new to java. Please help.

Rowdyism answered 5/7, 2013 at 8:48 Comment(4)
Is the driver jar in your classpath ?Mcgurn
ensure that you have synchronized on your getInstance()Egoism
Possible duplicate of ClassNotFoundException com.mysql.jdbc.DriverRanaerancagua
You need to paste the jar file into your webserver's lib directory, if you are getting the error in runtime. whereas, if you are getting the error on compile time add the jar in build path.Kelantan
K
153

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

  • MAVEN PROJECTS SOLUTION

Add the mysql-connector dependency to the pom.xml project file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

  • ALL PROJECTS SOLUTION

Add the jar library manually to the project.

Right Click the project -- > build path -- > configure build path

In Libraries Tab press Add External Jar and Select your jar.

You can find zip for mysql-connector here

  • Explanation:

When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

Kessel answered 5/7, 2013 at 9:0 Comment(2)
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ?Althaalthea
Problem won't get solved until you add mysql-connector-java-8.0.15.jar file into apache-tomcat-9.0.16\lib folder, as suggested by @Asad.Mcpeak
P
57

If you got the error in your IDE(compile-time error), you need to add your mysql-connector jar file to your libs and add this to your referenced library of project too.

If you get this error when you are running it, then probably its because you have not included mysql-connector JAR file to your webserver's lib folder.

Add mysql-connector-java-5.1.25-bin.jar to your classpath and also to your webserver's lib directory. Tomcat lib path is given as an example Tomcat 6.0\lib

Plasticizer answered 5/7, 2013 at 10:56 Comment(2)
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ?Althaalthea
if you are not using any IDE then you should add jar file path to your classpathPlasticizer
C
28

Every one has written an answer but I am still surprised that nobody actually answered it by using the best simple way.
The people answer that include the jar file. But, the error will still occur.

The reason for that is, the jar is not deployed when the project is run. So, what we need to do is, tell the IDE to deploy this jar also.

The people here has answered so many times that put that jar file in the lib folder of WEB-INF. That seems okay, but why do it manually. There is simple way. Check the below steps:

Step 1: If you haven't referenced the jar file into the project then, reference it like this.

Right click on the project and go to the project properties. Then, go to the java build path, then add external jar file via that.

But this will still not solve the problem because adding the external jar via build path only helps in compiling the classes, and the jar will not be deployed when you run the project. For that follow this step

Right click on the project and go to the project properties. Then, go to the Deployment Assembly then press Add , then go to the java build path entries and add your libraries whether it is jstl, mysql or any other jar file. add them to deployment. Below are the two pictures which display it.

Before Adding

After Adding

Cumine answered 11/12, 2018 at 10:57 Comment(0)
C
15

For Gradle-based projects you need a dependency on MySQL Java Connector:

dependencies {
    compile 'mysql:mysql-connector-java:6.0.+'
}
Capote answered 11/3, 2015 at 13:28 Comment(1)
What ended up working for me was runtime 'mysql:mysql-connector-java:6.0.+'.Suberin
G
14

You will have to include driver jar for MySQL MySQL Connector Jar in your classpath.

If you are using Eclipse: How to add dependent libraries in Eclipse

If you are using command line include the path to the driver jar using the -cp parameter of java.

java -cp C:\lib\* Main
Goetz answered 5/7, 2013 at 8:51 Comment(2)
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ?Althaalthea
Using -cp gives Could not find or load main class error. Do I need to put Main class to another folder?Oath
D
13

check for jar(mysql-connector-java-bin) in your classpath download from here

Doff answered 5/7, 2013 at 8:51 Comment(0)
A
9

JDBC API mostly consists of interfaces which work independently of any database. A database specific driver is required for each database which implements the JDBC API.

First download the MySQL connector jar from www.mysql.com, then:

Right Click the project -- > build path -- > configure build path

In the libraries tab press Add External Jar and select your jar.

Antechamber answered 22/8, 2014 at 10:21 Comment(0)
A
5

For Maven based projects you need a dependency.

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
</dependency>
Anyone answered 20/2, 2015 at 12:3 Comment(0)
G
3

The driver connector is not in your build path. Configure the build path and point it to the 'mysql-connector-java-5.1.25-bin.jar' (check the version which you are using). Alternatively you can use maven :D

Grilse answered 11/11, 2014 at 5:33 Comment(0)
N
2

In the project into the folder Libraries-->right click --> Add Library --> Mysqlconnector 5.1

Nickienicklaus answered 25/4, 2014 at 17:50 Comment(0)
D
2

For IntelliJ Idea, go to your project structure (File, Project Structure), and add the mysql connector .jar file to your global library. Once there, right click on it and chose 'Add to Modules'. Hit Apply / OK and you should be good to go.

Donohoe answered 21/7, 2018 at 19:2 Comment(0)
E
2

This needs to be used as of 2021

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
Ebullient answered 7/12, 2019 at 10:24 Comment(0)
T
1

Trivial as it may seem in my case netbeans version maven project 7.2.1 was different. There is a folder in the project called dependencies. Right click and then it brings up a popup window where you can search for packages. In the query area put

mysql-connector

It will bring up the matches (it seems it does this against some repository). Double click then install.

Tangerine answered 19/12, 2013 at 4:6 Comment(0)
K
1

It is because the WEB-INF folder does not exist at the location in the sub directory in the error. You either compile the application to use the WEB-INF folder under public_html OR copy the WEB-INF folder in sub folder as in the error above.

Kellie answered 13/3, 2014 at 6:38 Comment(0)
C
1

I was having the same issue. I was using intellijj IDE for creating the MySQL connection.

Steps to fix it:

  1. Download: mysql-connector.jar( I used 8.0.29).

  2. Go to "file-->project structure -->Libraries-->Click on plus button and select java and select the jar file you downloaded in step 1". enter image description here

  3. Check the jar file is showing under "External Libraries directory"

enter image description here 4. Now try to create the connection. It will work.

I used this code for creating MySQL connection:

void createConnection() throws SQLException, ClassNotFoundException {
    Connection connection=null;
    try {

        Class.forName("com.mysql.cj.jdbc.Driver");
          connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/candelete"
                ,"root","");
        System.out.println("Connection created");
        System.out.println("hashcode is: "+connection.hashCode());
    }
    finally {
        System.out.println("here");
        System.out.println("hashcode is: "+connection.hashCode());
        connection.close();
        System.out.println("hashcode now: "+connection.hashCode());
    }
}
Cathodoluminescence answered 3/5, 2022 at 17:0 Comment(0)
D
0

The exception can also occur because of the class path not being defined.

After hours of research and literally going through hundreds of pages, the problem was that the class path of the library was not defined.

Set the class path as follows in your windows machine

set classpath=path\to\your\jdbc\jar\file;.
Deckhand answered 26/3, 2016 at 0:42 Comment(0)
H
0

I Understood your problem add this dependency in your pom.xml your problem will be solved,

https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38

Hyetography answered 23/6, 2016 at 13:0 Comment(0)
C
0

If you are using tomcat then along with project directory you should also copy the database connector jar file to tomcat/lib. this worked for me

Capri answered 7/2, 2017 at 20:11 Comment(0)
E
0

I'm developing a simple JavaFX11 application with SQLite Database in Eclipse IDE. To generate report I added Jasper jars. Suddenly it throws "THIS" error.

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

It was running good (BEFORE THIS ADDITION). But suddenly!

I'm not using maven or other managers for this simple application. I'm adding jars manually.

I created "User Library" and added my jars from external folders.

PROBLEM OCCURING AREA: My "User Library" are marked as system library. I just removed the marking. Now its not a system library. "NOW MY PROJECT WORKING GOOD".

DEBUG MYSELF: Tried other things: AT RUN CONFIGURATION: Try, removing library and add jars one-by-one and see. - here you have to delete all jars one by one, there is no select all and remove in eclipse right now in run configuration. So the error messages changes form one jars to another.

Hope this helps someone.

Estrus answered 26/2, 2020 at 6:24 Comment(0)
D
0
I was also facing the same problem

Download mysql-connector-java jar file

paste it in the C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib

Hope this work for you also !!

Devereux answered 26/11, 2020 at 11:21 Comment(0)
C
0

Finally

I solved by two steps :

1 - add the below to pom.xml

  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.8</version>
        </dependency>

2 - Download jar file from this URL:https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.0.8 after that put it in your tomcat/lib folder.

Cosimo answered 2/3, 2022 at 13:8 Comment(0)
P
0

I just had to do a maven reload in my Intellij. I figured this out when I checked the maven dependencies inside Intellij and found that they were not reflecting my latest pom changes.

Polyunsaturated answered 20/10, 2023 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.