ClassNotFoundException com.mysql.jdbc.Driver [duplicate]
Asked Answered
A

21

47

This question might have asked here number of times . After doing some google search for the above error and doing some update, I can't understand why I'm still getting that error. I've already put my driver-- mysql-connector-java-5.1.5-bin in the classpath:

Java_Home\jre\lib\
Java_Home\jre\lib\ext\
Java_Home\lib

and the code which I'm using to connect to mysql database is:

try{
Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail","root","");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select message_body from deadletter");
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} 

con.close();

}
catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        }

    }

and the complete stacktrace of the above exception is:

java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)  
    at java.lang.Class.forName(Class.java:169)
    at mail.main(mail.java:114)  

Now, what's wrong I'm doing here?

Andvari answered 18/10, 2009 at 19:34 Comment(4)
So, what exactly does your classpath look like? You must put the JAR file (mysql-connector-java-5.1.5-bin.jar) in the classpath.Kilocycle
The exception message has a colon in the class name, which I don't believe should happen.Dann
The Class.forName() line has been obsolete since JDBC 4 in 2007, and whatever MySQL Connector first implemented it.Composition
Ran into this in a Spring Boot project and resolved by including this in the Gradle dependencies block: runtime('mysql:mysql-connector-java')Candelabra
C
24

The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver

Chantal answered 18/10, 2009 at 19:42 Comment(1)
@Chantal I thought that the driver went with Apache James itself.Squiffy
B
42

If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:

  1. Right click your project folder and open up Properties.

  2. From the right panel, select Java Build Path then go to Libraries tab.

  3. Select Add External JARs to import the mysql driver.

  4. From the right panel, select Deployment Assembly.

  5. Select Add..., then select Java Build Path Entries and click Next.

  6. You should see the sql driver on the list. Select it and click first.

And that's it! Try to run it again! Cheers!

Barnett answered 4/10, 2015 at 18:24 Comment(4)
You saved my day man (actually a week!). Of all the solutions on the internet this was the one I needed. Strange ways of eclipse :->Resorcinol
THANK YOU SO MUCH! I struggled with this problem for over 2 hours, and this was the solution to the problem. A sincere thank you!Overstudy
This is the solution that worked for me as well. Specifically, steps 4-6 are the additional steps that resolved the database connectivity issue I was facing.Dyewood
Wow. Can't believe that after 2.5 years, my solution is still working for you guys. Code on!Barnett
M
27

I too struggled with the same problem and finally got the solution for it. Just copy the MySql-Connector.jar into Tomcat's lib folder, and then remove the jar from the webapp's lib folder, and then, run the project.

Menace answered 2/11, 2012 at 6:20 Comment(3)
This works . Yet this is not a good solution I guess. Can you suggest a more formal wayMerimerida
If you are using any IDE then the "Server Clean" will do this task. If you are not using IDE then you have to do this manually.Menace
Works like a charm!Hudak
C
24

The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver

Chantal answered 18/10, 2009 at 19:42 Comment(1)
@Chantal I thought that the driver went with Apache James itself.Squiffy
L
13

I have the same problem but I found this after a long search: http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-Load-Class.html

But I made some change. I put the driver in the same folder as my ConTest.java file, and compile it, resulting in ConTest.class.

So in this folder have

ConTest.class
mysql-connector-java-5.1.14-bin.jar

and I write this

java -cp .;mysql-connector-java-5.1.14-bin.jar ConTest

This way if you not use any IDE just cmd in windows or shell in linux.

Leftwich answered 28/12, 2010 at 0:11 Comment(3)
For linux, you would have to replace ' ; ' with ' : 'Karmenkarna
work for me! thank you so much, you guys save my weekend!Cudgel
Are you sure this works? It gives that the term mysql-connector-java-5.1.14-bin.jar is not recognized as the name of cmdlet.Folkway
U
6

this ans is for eclipse user......

first u check the jdbc jar file is add in Ear libraries....

if yes...then check...in web Content->web Inf folder->lib

and past here jdbc jar file in lib folder.....

Uncrown answered 25/4, 2017 at 8:16 Comment(1)
Solved my problem in 2020 :=)... Thanks..Theatre
M
4

1) Download connector from here https://www.mysql.com/products/connector/

2) Select JDBC driver for mysql

3) click on Platform Independent (Architecture Independent), ZIP Archive

4) Download the file and unzip it

5) (For Eclipse)Click Project->properties-> Java Build Path->Libraries (For Netbeans) right click libraries on left bar-> add jar

6) Click on add external jar

7) select mysql-connector-java-5.1.40-bin.jar

8) Done!

Mob answered 5/2, 2017 at 8:57 Comment(0)
S
4

If you are using NetBeans follow the following steps:

  1. Right click on 'Libraries' and select 'Add Library..'
  2. Scroll down the list of 'Global Libraries' and select 'MySQL JDBC Driver' and click on 'Add Library'.
Starbuck answered 13/3, 2017 at 9:29 Comment(0)
P
3

What did you put exactly in lib, jre/lib or jre/lib/ext? Was it the jar mysql-connector-java-5.1.5-bin.jar or something else (like a directory)?

By the way, I wouldn't put it in lib, jre/lib or jre/lib/ext, there are other ways to add a jar to the classpath. You can do that by adding it explicitly the CLASSPATH environment variable. Or you can use the -cp option of java. But this is another story.

Penetrating answered 18/10, 2009 at 20:17 Comment(0)
H
2

In NetBeans,

  1. right click on libraries, select properties
  2. select rub tab
  3. there you need to add the mysql-connector-java.jar and apply
Hom answered 27/5, 2016 at 2:25 Comment(0)
I
2

You should download MariaDB connector.
Then:

  1. Expand your project.
  2. Right-click on Libraries.
  3. Select Add Jar/Folder.
  4. Add mariadb-java-client-2.0.2.jar which you just downloaded.
Igal answered 19/6, 2017 at 4:27 Comment(0)
D
1

Ok, i find solution changing the path of mysql-conector-java.jar to the follow path:

ProjectName/WebContent/Web-inf/lib/mysql-conector-java.jar 

So you need to add the conector to project again and delete the previous one.

Damson answered 17/12, 2013 at 19:43 Comment(0)
P
1

Most of the possible solution has been covered above. From my experience of this issue, i have placed the mysql-connector-jar in the /WEB-INF/lib folder of the webapp module and it worked fine for me.

Paxwax answered 27/5, 2016 at 3:9 Comment(0)
C
1

In netbean Right click on your project, then you click on Libraries, then Run tab, add library, then select mysql connector

Candleberry answered 8/9, 2017 at 14:42 Comment(0)
V
1

In IntelliJ Do as they say in eclipse "If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:

Right click your project folder and open up Properties.

From the right panel, select Java Build Path then go to Libraries tab.

Select Add External JARs to import the mysql driver.

From the right panel, select Deployment Assembly.

Select Add..., then select Java Build Path Entries and click Next.

You should see the sql driver on the list. Select it and click first.

And that's it! Try to run it again! Cheers!"

Here we have to add the jar file in Project Structure -> Libraries -> +(add)

Vera answered 2/6, 2018 at 23:4 Comment(0)
B
0

I keep the mysql-connector jar with my project rather than in Javahome. As a result, you can be sure it can be found by being sure its in the local classpath. A big upside is that you you can more the project to another machine and not have to worry about (or forget) to set this up again. I personally like including it in version control.

Bodine answered 18/10, 2009 at 20:2 Comment(0)
D
0

Ok..May be i can also contribute my solution.. Right click on project -properties -->Deployment assembly...there you need to add the mysql-connector-java.jar and apply it...which makes your prject configured with web-Libraries that has this sql connector...This worked for me.. I hope this works for you guys as well

Dinner answered 9/5, 2014 at 5:55 Comment(0)
D
0

The only thing that worked for me was downloading the mysql-connector-java-5.0.8-bin.jar directly from the MySQL website:

http://dev.mysql.com/downloads/connector/j/3.1.html

Then if you're using Eclipse paste this mysql-connector-java-5.0.8-bin.jar in the WEB-INF lib folder

If doesn't works try with any of the solutions posted in this link: http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html

Danette answered 6/4, 2015 at 23:31 Comment(0)
S
0

I experienced the same error after upgrading my java to 1.8.0_101. Try this: i. Remove the mysql.jar fro your buildpath. ii. Clean the server and project iii. Add the jar file back to the build path.

Worked for me.

Serbocroatian answered 25/7, 2016 at 6:21 Comment(0)
G
0

Just copy the MySQL JDBC drive jar file and paste it to Tomcat or whatever is the server's lib folder. It works for me.

Geter answered 7/12, 2016 at 18:43 Comment(0)
D
0

just copy and paste the mysqlconnector jar to your project folder and then build path it will definitely work.

Discomposure answered 15/4, 2018 at 20:37 Comment(0)
J
-1

see to it that the argument of Class. forName method is exactly "com. mysql. jdbc. Driver". If yes then see to it that mysql connector is present in lib folder of the project. if yes then see to it that the same mysql connector . jar file is in the ClassPath variable (system variable)..

Junitajunius answered 9/1, 2015 at 11:44 Comment(1)
No spaces: "com.mysql.jdbc.Driver"Vamp

© 2022 - 2024 — McMap. All rights reserved.