Java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

What is wrong with the code, there are many errors during debugging. I am writing code for a singleton class to connect to mysql database.

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.

+72
java jdbc mysql-connector
Jul 05
source share
16 answers

It seems that the mysql connection library is not included in the project. Solve the problem by following one of the suggested solutions:

  • SOLUTION OF MAJOR PROJECTS

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 versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

  • ALL DRAFT SOLUTIONS

Add the jar library manually to the project.

Right click on the project -> build path -> configure build path

In the Libraries Tab click Add External Jar and Select your jar.

You can find the zip for mysql-connector here

  • Explanation:

When creating a project, java throws you an exception because the file (class com.mysql.jdbc.Driver) from the mysql connection library was not found. The solution adds the library to the project, and java finds com.mysql.jdbc.Driver

+110
Jul 05 '13 at 9:00
source share

If you encounter an error in your IDE (compile-time error), you need to add the jar file of the file to the mysql connector in your libraries and add it to your project reference library.

If you get this error when it starts, it may be due to the fact that you did not include the mysql-connector JAR file in your web server lib folder.

Add mysql-connector-java-5.1.25-bin.jar to your classpath, as well as to your lib web directory. Tomcat lib path is an example of Tomcat 6.0\lib

+50
Jul 05 '13 at
source share

You will need to include the jar for MySQL MySQL Connector Jar in your classpath.

If you use Eclipse: How to add dependent libraries in Eclipse

If you use the command line, specify the path to the driver container using the -cp parameter for java.

 java -cp C:\lib\* Main 
+11
Jul 05 '13 at 8:51
source share

For project-based Gradle, you need a dependency on the MySQL Java Connector :

 dependencies { compile 'mysql:mysql-connector-java:6.0.+' } 
+11
Mar 11 '15 at 13:28
source share

The JDBC API mainly consists of interfaces that operate independently of any database. Each database that implements the JDBC API requires a specific database driver.

First download the MySQL Connector banner from www.mysql.com, then:

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

On the Libraries tab, click Add External Jar and select a jar.

+8
Aug 22 '14 at 10:21
source share

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

+7
Jul 05 '13 at 8:51
source share

In the project, in the Libraries folder → right-click → Add Library → Mysqlconnector 5.1

+2
Apr 25 '14 at 17:50
source share

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

+2
Nov 11 '14 at 5:33
source share

For Maven based projects, you need a dependency.

 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> 
+2
Feb 20 '15 at 12:03
source share

It is trivial, as it may seem in my case, the version of netbeans version of maven 7.2.1 was different. The project has a folder called dependencies. Right-click and then a popup will appear in which you can search for packages. In the request area, put

 mysql-connector 

It will output matches (it looks like it is doing it against some repository). Double click and install.

+1
Dec 19 '13 at 4:06 on
source share

This is because the WEB-INF folder does not exist in the location in the error subdirectory. You either compile the application to use the WEB-INF folder in public_html OR copy the WEB-INF folder to a subfolder, as in the error above.

+1
Mar 13 '14 at 6:38
source share

Everyone wrote an answer, but I'm still surprised that nobody really answered it using the best easy way.
People respond that they include a JAR file. But the error will still occur.

The reason for this is that the JAR is not deployed when the project starts. So what we need to do is tell the IDE to deploy this jar as well.

People here have answered so many times that they put this jar file in the lib folder on WEB-INF. It looks good, but why do it manually. There is an easy way. Check the following steps:

Step 1: If you have not already referenced the jar file in the project, make a link to it as follows.

Right-click on the project and go to the project properties. Then go to the Java build path, and then add the external JAR file through it.

But this still will not solve the problem, because adding an external jar file through the build path helps only when compiling classes, and the jar will not be deployed when the project starts. To do this, complete this step.

Right-click on the project and go to the project properties. Then go to the Deployment Assembly, then click Add , then go to the Java build path entries and add your libraries, be it jstl, mysql or any other jar file. add them to the deployment. Below are two pictures that display this.

Before adding

After adding

+1
Dec 11 '18 at 10:57
source share

An exception may also occur because the class path is undefined.

After several hours of research and literally hundreds of pages passing, the problem was that the class path to the library was undefined.

Set the class path as follows on your Windows computer.

 set classpath=path\to\your\jdbc\jar\file;. 
0
Mar 26 '16 at 0:42
source share

I understood your problem by adding this dependency to your pom.xml, your problem will be solved,

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

0
Jun 23 '16 at 13:00
source share

If you use tomcat and then along with the project directory, you must also copy the database connector file jar file to tomcat / lib. it worked for me

0
Feb 07 '17 at 20:11
source share

For IntelliJ Idea, go to your project structure (file, project structure) and add the mysql .jar connector file to your global library. After that, right-click on it and select "Add to Modules". Click Apply / OK and you should be good to go.

0
Jul 21 '18 at 19:02
source share



All Articles