21

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?

gresdiplitude
  • 1,665
  • 1
  • 15
  • 27

10 Answers10

27

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.

aizaz
  • 3,056
  • 9
  • 25
  • 57
  • Why won't it read from the Libraries > Referenced Libraries? As adding to Build Path adds to this folder only. And if you try importing the class directly in your code that works? – Prateek Pande Jan 25 '21 at 06:15
16

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

user2528322
  • 161
  • 1
  • 2
5

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.

Castrohenge
  • 8,525
  • 5
  • 39
  • 66
user2290626
  • 51
  • 1
  • 1
  • i m also getting this same error how can i set classPath is it related to set CLASSPATH variable in environment variables ? – Erum Aug 21 '14 at 05:59
4

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

Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
2

Include path of jar (jdbc driver) in classpath.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Ankit
  • 6,554
  • 6
  • 49
  • 71
2

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

Jonny Right
  • 528
  • 4
  • 10
2

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>
Ranjith Sekar
  • 1,892
  • 2
  • 14
  • 18
1

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.

JL_SO
  • 1,742
  • 1
  • 25
  • 38
0

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/

0

I had

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

Changed to

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

Fixed my problem

Jason Portnoy
  • 757
  • 8
  • 23