[java] java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

I'm getting the exception java.lang.ClassNotFoundException when I am trying to run my code,

My Code

   try 
   {
     Class.forName("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/simple",
      "root","root");
     Statement stmt=con.createStatement();
     String query="SELECT * FROM CUST";
     ResultSet rs=stmt.executeQuery(query);
     while(rs.next())
     {
          System.out.print(rs.getString("CUST_NAME") +" ");
          System.out.print(rs.getString(2) +" ");
          System.out.print(rs.getString(3) +" ");

     }    
     rs.close();
     stmt.close();
     con.close();    
  }   
  catch (ClassNotFoundException e) 
  {
     e.printStackTrace();
  }
  catch (SQLException e) 
  {
     e.printStackTrace();
  }    

I'm getting Error

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:188)
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 Simple.MyProg.main(MyProg.java:15)   

What am I doing wrong?

This question is related to java exception

The answer is


problem is not in the code, but you don't have added the driver to your project!!! You have to add the *.jar driver to your project...

Try putting this in your lib directory, then re-starting tomcat...

problem is Class.forName("com.mysql.jdbc.Driver"); it tries to load the driver, but it is not getting it, this is the reason you are getting:

java.lang.ClassNotFoundException.


If you get this error when you are running it, then probably its because you have not included mysql-connector JAR file to your webserver's lib folder.

Now it is important to add mysql-connector-java-5.1.25-bin.jar to your classpath and also to your webserver's lib directory. Tomcat lib path is given as an example Tomcat 6.0\lib


If you are using an eclipse ide, download the mysql jdbc connector jar and point that jar to the build path. Project Java Build Path --> Libraries --> Add external jars. Connector can be obtained from http://dev.mysql.com/downloads/connector/j/


Copyed the *.jar into my WEB-INF/lib folder -> Worked for me. When including over buildpath there was everytime this errormsg.


You can download the latest mysql driver jar from below path, and copy to your classpath or if you are using web server then copy to tomcat/lib or war/web-inf/lib folder.

http://dev.mysql.com/downloads/connector/j/ or

http://mirrors.ibiblio.org/pub/mirrors/maven2/mysql/mysql-connector-java/5.1.10/mysql-connector-java-5.1.10.jar


Include path of jar (jdbc driver) in classpath.


Note that the name of the Driver class in mySQL v8 jar (at time of writing mysql-connector-java-8.0.12.jar) has changed to com.mysql.cj.jdbc.Driver You can verify this for yourself by going into the jdbc jar and looking at the META-INF/services/java.sql.Driver file.


if it is standalone program, download mysql connector jar and add it to your classpath.

if it is a maven project, add below dependency and run your program.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.37</version>
</dependency>

I had

runtime('mysql:mysql-connector-java')

Changed to

compile('mysql:mysql-connector-java')

Fixed my problem


The Problem is related to MySql Driver

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

Add the MySQL jdbc driver jar file in to your classpath.

Also i have this error on JDK. I build the ClassPath Properly then I put the "mysql-connector-java-5.1.25-bin" in dir "C:\Program Files\Java\jre7\lib\ext" in this dir i have my JDK. then compile and Run again then it's working fine.