Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC Tutorial - Writing first JDBC example and running in Eclipse

In this JDBC tutorials series you will learn about JDBC and write first JDBC code to insert data into MySQL Database.


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

How to create your first JDBC Example?

Beginners in JDBC will first learn how to write simple JDBC program to connection to database and then perform insert operation. In this example on website we are using the MySQL database server as backend.

We have also included the video instruction of creating project in Eclipse, downloading JDBC driver for MySQL, adding JDBC driver jar file to the project, writing the JDBC code and finally executing the program.

Requirements for first JDBC program:

  1. JDBC 1.6 or above should be installed and configured on your computer
  2. Eclipse IDE should be installed and configured.
  3. MySQL database should be running
  4. MySQL JDBC driver (to be downloaded from MySQL website)

JDBC is the specification in Java for connecting to the database and performing the database operations. JDBC simply allows you to run your query against database. So, in your Java program you can write sql for various operations such as inserting data, deleting data, selecting data and updating the data, then you can execute these query against database using the JDBC.

Here is the video instruction to create and run first JDBC Example.

Steps to write and run JDBC program

Step 1: Create Eclipse project

I am assuming that you have Eclipse installed and running perfectly. Open the Eclipse IDE and create a new project as explained in the video above.

Step 2: Download the JDBC driver for MySQL.  You can download it from After downloading the zip file extract it and then add the jar file into project as explained in the video.

Step 3: Create the database and table.

If you don't have database in the MySQL server then create one using the following command:

create database jdbc_example;

Now use the following sql to create the table:

CREATE TABLE `employee` (               
	`id` int(11) NOT NULL DEFAULT '0',    
	`name` varchar(30) DEFAULT NULL,      
	`salary` int(11) DEFAULT NULL,        
	PRIMARY KEY (`id`)                    

Step 4: Create Java code and run example

Create a new Java class (InsertValues .java) in project and add the following code:

package net.roseindia;

import java.sql.*;

public class InsertValues {
public static void main(String[] args) {
	System.out.println("Inserting values in Mysql database table!");
	Connection con = null;
	String url = "jdbc:mysql://localhost:3306/";
	String db = "jdbc_example";
	String driver = "com.mysql.jdbc.Driver";
	try {
	con = DriverManager.getConnection(url + db, "root", "root");
	try {
		Statement st = con.createStatement();
		String sql = "INSERT employee (id,name,salary) VALUES(" + 1 + ","
		+ "'Deepak Kumar'," + "1000)";
		int val = st.executeUpdate(sql);
		System.out.println("1 row affected");
	} catch (SQLException s) {
		System.out.println("SQL statement is not executed! Error is: " + s.getMessage());
	} catch (Exception e) {

The code Class.forName(driver); is used to load the MySQL JDBC driver.

The code con = DriverManager.getConnection(url + db, "root", "root"); is used to connect to the MySQL Database.

The code Statement st = con.createStatement(); is used to get the Statement object. The statement object is used to run the query against database.

The code st.executeUpdate(sql); is used to run the sql against database.

And now finally you can run it from the Eclipse IDE.

In this section you created a JDBC program to insert the data into database. 

Download the source code of JDBC Example discussed here.


Share on Google+Share on Google+

JDBC Tutorial - Writing first JDBC example and running in Eclipse

Posted on: November 8, 2014 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JDBC Tutorial - Writing first JDBC example and running in Eclipse  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image