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$ at$ at Method) at at java.lang.ClassLoader.loadClass( at sun.misc.Launcher$AppClassLoader.loadClass( at java.lang.ClassLoader.loadClass( at java.lang.Class.forName0(Native Method) at java.lang.Class.forName( at com.gk.JDBCConnection.main(

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