How to add jars to a project in Eclipse:

Create a simple project in Eclipse.

create simple project

In this project we are connecting to the MySQL DB.

Shown as below:

package com.gk.dbconnection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class ConnectionClass { public static void main(String[] args) { Connection conn = null; try { Class.forName("com.mysql.jdbc.driver"); conn =DriverManager.getConnection("jdbc:mysql:localhost ://3306/geeklabs","root","root"); //here geeklabs is dbname,root is username,password System.out.println("connection successfully"); Statement st = conn.createStatement(); String s = "create table tableName(sid int(10),sname varchar(25))"; System.out.println("table created"); String s1 = "insert into stuednt value(10,'srikanth')"; st.executeUpdate(s1); System.out.println("values are inserted"); } catch (ClassNotFoundException ce) { ce.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } } }

Now run ConnectionClass, you get ClassNotFoundException at runtime.

java.lang.ClassNotFoundException: com.mysql.jdbc.driver at$ Source) at$ Source) at Method) at Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.gk.dbconnection.ConnectionClass.main(

Why we are getting this type of exception means, the Mysql jdbc driver jar is not found at runtime. Solution for this problem is to add Mysql jdbc driver jar to project.

How to add jar’s in Eclipse:

Go to project right click on the project then select the option that is Build Path click on that option and it shows an another options, click on the Configure Build Path shown below

buid path configuration

After click on the Configure Build Path … option generating a new window that is ‘ Properties for Connection’ in that window select the Add External JARs option after that click on the Install New Software … option then it will be open the computer locations. Before going to do this first download the jars. If you don’t have the libraries jars, go to Internet and download the required jars. Then save those downloaded jars into your favorite location in system. Then click on the Add External JARs … Now go to were you store the Libraries jars then select the jars and click on the Ok button.

in libraries option add external jars

After click on the Add External JARs ... Button it will be open like

added external jar will be store in jar storage location

In my case, I saved the libraries in this location. Now I want to Mysql Connector jar select click on the open button. The selected jar is shows in the ‘Libraries’ Like shown below:

added external jar will be store in jar storage location

Then click on the select and click on the Ok button. Now go to run the project.

run the project

Now we won’t get any Exception, why because we added the jar. In console it will be printed like shown below.

in console It will print 'connection successfully'

This is the process to adding the jars to project in Eclipse.

HTML Comment Box is loading comments...