MySql Driver ClassNotFoundException

Creating a simple program to connect the Mysql DatBase. In this program you can connect to the Mysql DataBase, utilizing JDBC Driver.

						
import javax.naming.spi.DirectoryManager; public class JDBCConnection { public static void main(String[] args) { Connection conn = null; try { // Step1:Loading the driver Class.forName("com.mysql.jdbc.Driver"); // Step2: Connection Establishment conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Data", "root", "root"); System.out.println("connection Established"); conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException ce) { ce.printStackTrace(); } } }

When you run this program, you will get the Exceptions like ClassNotFoundException. Why because Mysql Database jar’s are not added to the current project classpath.

					
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:186) at com.gk.JDBCConnection.main(JDBCConnection.java:14)

Inorder to add the Jar’s to this project. Right click on the project choose the Build path in that select the Configure Build path. The description of process is shown below.

Buildpath option

It will be open like below in that select the Libraries tab then click on the Add Extranal JARs…

adding external jars

Go to where the Mysql jar location, in my case adding the jar to current project classpath

selecting jar from localsystem

Then click on the Ok button

Java Buildpath

Lets now run the program again shown below

Class Not Found Exception

The program runs without Exception

Connection also Established which shows output in console

Resolvig Exception
HTML Comment Box is loading comments...