JDBC Video Tutorial- How to insert data into MySQL database table?

This video tutorial is for beginners in JDBC which teaches the new comers to write Java program to insert data into database.

JDBC Video tutorial example for inserting data into MySQL database

JDBC programming is the main thing a developer should learn after learning the Core Java and in this video I have explained the steps of write code to insert the data into MySQL database.

We have used the MySQL database as it is easy to download and install, but you can easily modify this program to run against database. You have to change the JDBC driver for that particular database and the program remains the same.

How the program works?

  1. Program uses the MySQL database to connect to the MySQL database
  2. The Statement class of the JDBC api is used to run a sql statement against the database
  3. We have used the query “INSERT employee (id,name,salary) VALUES(1,'Deepak','10000')” to insert the database into database.
  4. The executeUpdate() method of the statement object runs the query against database and database is inserted into database.
  5. The try and catch exception block is used to catch exceptions if any while executing the program.

Here is the code of creating the database 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 

Video Tutorial: How to insert data into MySQL Table?

Here is the code of the full Java program:

package net.roseindia.jdbcexamples;

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 = "jdbcexamples";
		String driver = "com.mysql.jdbc.Driver";
		try {
			con = DriverManager.getConnection(url + db, "root", "root");
			try {
				Statement st = con.createStatement();
				int val = st
		.executeUpdate("INSERT employee (id,name,salary) VALUES(1,'Deepak','10000')");
				System.out.println("Data added");
			} catch (SQLException s) {
				System.out.println("SQL statement is not executed!");

		} catch (Exception e) {

To run the program right click in the java code window and then select Run As-> Java Application

After successful execution of the program record will be inserted into the MySQL table.

Briefly following are the steps to insert into database:

  1. Connect to MySQL Database using JDBC
  2. Create the Statement Object
  3. Construct the sql
  4. Run the sql using the Statement object against database
  5. Process results if any

Here is the sql script to create table:

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

So, in this tutorial on RoseIndia.net you have learned the example program for inserting the data into MySQL database using the JDBC driver.

Download the source code in the Eclipse Project format.

You can find more details at http://www.roseindia.net/jdbc/jdbc-mysql/InsertValues.shtml

Check all JDBC Tutorials at http://www.roseindia.net/jdbc/index.shtml