0

ClassNotFoundException in JDBC program

I am writing a simple program in Java using to demonstrate insertion of data in MySQL table using JDBC. But the program is generating ClassNotFoundException while registering driver. import java.sql.*; public class test { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) { System.out.println("ClassNotFoundException: "+e.getMessage()); } } } I have added the driver JAR file in the same directory. To compile the program: javac -cp . test.java To execute the program: java -cp . test O/p: ClassNotFoundException: com.mysql.jdbc.Driver

4th Feb 2021, 6:04 PM
Shashank Shekhar
Shashank Shekhar - avatar
5 Réponses
+ 2
Shashank Shekhar You have to put jdbc jar file in the lib folder of JDK
4th Feb 2021, 6:30 PM
A͢J
A͢J - avatar
+ 2
You most likely haven't added the jar to the classpath. Usually, a projects jar files are placed within a directory within the projects root named lib. https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html https://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html?m=1
4th Feb 2021, 6:36 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Or right click on the project, go to build path, click add external jars, select your mysql-connector jar file and give apply.
4th Feb 2021, 7:51 PM
Avinesh
Avinesh - avatar
0
I am not using any IDE but I have put the jar file in the projects root directory. Then also this is happening.
5th Feb 2021, 2:14 AM
Shashank Shekhar
Shashank Shekhar - avatar
0
I Am AJ ! I have put the jar file in lib folder of jdk then also this is happening.
5th Feb 2021, 3:52 AM
Shashank Shekhar
Shashank Shekhar - avatar