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

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

查看更多
裙下三千臣
3楼-- · 2018-12-31 10:54

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.

查看更多
怪性笑人.
4楼-- · 2018-12-31 10:55

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.....

查看更多
浪荡孟婆
5楼-- · 2018-12-31 10:56

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.

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

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.

查看更多
无与为乐者.
7楼-- · 2018-12-31 11:01

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!

查看更多
登录 后发表回答