ClassNotFoundException com.mysql.jdbc.Driver

2018-12-31 10:32发布

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?

21条回答
不流泪的眼
2楼-- · 2018-12-31 10:37

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

查看更多
笑指拈花
3楼-- · 2018-12-31 10:37

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.

查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 10:37

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

查看更多
只靠听说
5楼-- · 2018-12-31 10:38

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

查看更多
十年一品温如言
6楼-- · 2018-12-31 10:41

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.
查看更多
看风景的人
7楼-- · 2018-12-31 10:41

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.

查看更多
登录 后发表回答