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:41

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.

查看更多
何处买醉
3楼-- · 2018-12-31 10:41

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.

查看更多
谁念西风独自凉
4楼-- · 2018-12-31 10:47

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
查看更多
余生无你
5楼-- · 2018-12-31 10:47

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)

查看更多
爱死公子算了
6楼-- · 2018-12-31 10:48

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'.
查看更多
孤独寂梦人
7楼-- · 2018-12-31 10:51

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!

查看更多
登录 后发表回答