JDBC Insert Record



In the previous series of the tutorials, we have learnt about creation of the table in MySQL database using the JDBC.

In this example, we are looking into the JDBC database tutorial to create a record in the table from the selected MySQL Database

JDBC Insert Record in Database :

package com.c4learn.jdbc;

import java.sql.*;

public class JDBCInsertRecord {
  // JDBC driver name and database URL
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB = "jdbc:mysql://localhost/student";

  // 1. Database User name & Password
  static final String USER = "admin";
  static final String PWD = "password";

  public static void main(String[] args) {
    Connection con = null;
    Statement stmt = null;
    try {
      // 2. Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      // 3. Open a connection
      System.out.println("Connecting to database...");
      con = DriverManager.getConnection(DB, USER, PWD);

      // 4. Execute a query
      System.out.println("Inserting record..");
      stmt = con.createStatement();

      String sql = "INSERT INTO user "
          + "values (1,'Ram','Naik',20)";
      stmt.executeUpdate(sql);

      sql = "INSERT INTO user "
          + "values (2,'Sam','George',23)";
      stmt.executeUpdate(sql);

      sql = "INSERT INTO user "
          + "values (3,'Anil','Pate',21)";
      stmt.executeUpdate(sql);

      System.out.println("Record inserted..");

    } catch (SQLException e) {
      System.out.println(e.getMessage());
    } catch (Exception e) {
      System.out.println(e.getMessage());
    } finally {
      try {
        System.out.println("Closing Connection");
        if (stmt != null)
          stmt.close();
      } catch (SQLException e) {
      }
      try {
        if (con != null)
          con.close();
      } catch (SQLException sqlEx) {
        System.out.println(sqlEx.getMessage());
      }
    }
    System.out.println("Ending Program..");
  }
}

Output :

Connecting to database...
Inserting record..
Record inserted..
Closing Connection
Ending Program..

Explanation : JDBC Insert Records

After the creation of the database in the MySQL, If we want to insert some records in the given table then we need to carry out following steps.

Step 1 : Importing the packages :

When we are dealing with the database connectivity then we need to include the required jar file which is necessary for the database programming.

import java.sql.*;

Step 2 : Registering JDBC driver :

After importing the packages we need to initialize a driver to open a communications channel with the database.

Class.forName("com.mysql.jdbc.Driver");

Step 3 : Open a connection .

DriverManager.getConnection() method is required to create a Connection object.

It represents a physical connection with database server. Below statement is used to create the DB URL –

static final String DB = "jdbc:mysql://localhost/student";

Using below statement we can connect to database physically.

System.out.println("Connecting to database...");
con = DriverManager.getConnection(DB_URL, USER,PWD);

Step 4 : Execute a query .

Below statement will create statement object for executing the query.

statement = con.createStatement();

We have created the following query –

String sql = "INSERT INTO user "
    + "values (1,'Ram','Naik',20)";
stmt.executeUpdate(sql);

sql = "INSERT INTO user "
    + "values (2,'Sam','George',23)";
stmt.executeUpdate(sql);

sql = "INSERT INTO user "
    + "values (3,'Anil','Pate',21)";
stmt.executeUpdate(sql);

To execute the query using the statement object we can write following line of code –

statement.executeUpdate(sql);

Using the statement object we can create a query and submit it to the database. This query will insert a row in specified table in the database.

Insert into Table in database

Step 5 : Clean up the environment .

After the execution of the query we need to clean up the resources. It requires explicitly closing all database resources

if (statement != null)
       statement.close();

and

if (con != null)
          con.close();