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 Tutorial - Writing first JDBC example and running in Eclipse

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 roseindia.net 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 http://www.mysql.com/products/connector/. 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`)                    
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

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 {
	Class.forName(driver);
	con = DriverManager.getConnection(url + db, "root", "root");
	try {
		Statement st = con.createStatement();
		String sql = "INSERT employee (id,name,salary) VALUES(" + 1 + ","
		+ "'Deepak Kumar'," + "1000)";
		System.out.println(sql);
		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) {
		e.printStackTrace();
	}
}
}

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.