JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

"INSERT INTO" Statements

This section describes how to insert data rows with INSERT INTO statements.

INSERT statements are used very often by database applications to insert data rows into tables. The syntax of INSERT statements is very simple. You need to provide a list of column names and a list of values for those columns. There are a couple of simple rules about INSERT statements:

  • You don't have to provide values to columns that have default values defined.
  • You don't have to provide values to columns that allow null values.
  • You should not provide values to IDENTITY columns, mainly used as primary key columns.

INSERT statements should be executed with the executeUpdate() method. Here is a simple program that insert some rows into my table Profile:

/**
 * DerbyMultipleInserts.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.util.*;
import java.sql.*;
public class DerbyMultipleInserts {
  public static void main(String [] args) {
    Connection con = null;
    try {
      con = DriverManager.getConnection(
        "jdbc:derby://localhost/TestDB");
      Statement sta = con.createStatement(); 
      int count = 0;

// insert a single row using default values
      count += sta.executeUpdate(
        "INSERT INTO Profile"
        + " (FirstName)"
        + " VALUES ('Herong')");

// insert a single row using provided values
      count += sta.executeUpdate(
        "INSERT INTO Profile"
        + " (FirstName, LastName, Point, BirthDate)"
        + " VALUES ('Janet', 'Gates', 999.99, '1984-10-13')");

// insert rows with loop with random values
      Random r = new Random();
      for (int i=0; i<10; i++) {
      	Float points = 1000*r.nextFloat();
      	String firstName = Integer.toHexString(r.nextInt(9999));
      	String lastName = Integer.toHexString(r.nextInt(999999));
        count += sta.executeUpdate(
          "INSERT INTO Profile"
          + " (FirstName, LastName, Point)"
          + " VALUES ('"+firstName+"', '"+lastName+"', "+points+")");
      }

// How many rows were inserted
      System.out.println("Number of rows inserted: "+count);

// Checking inserted rows
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("ID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getString("Point")
           + ", "+res.getDate("BirthDate")
           + ", "+res.getDate("ModTime"));
      }
      res.close();

      sta.close();
      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

Notice that Random class was used to generate some random strings and numbers. The output confirms that the insert statement was executed correctly:

C:\>java -cp .;\local\javadb\lib\derbyclient.jar DerbyMultipleInserts
Number of rows inserted: 12
List of Profiles:
  1, Herong, null, 0.0, 1988-12-31, 2006-12-31
  2, Janet, Gates, 999.99, 1984-10-13, 2006-12-31
  3, 144, 14859, 615.1516, 1988-12-31, 2006-12-31
  4, 21d6, efd17, 289.4137, 1988-12-31, 2006-12-31
  5, 977, 6779f, 766.5425, 1988-12-31, 2006-12-31
  6, 1002, e3873, 996.2754, 1988-12-31, 2006-12-31
  7, 1b2c, 8f798, 594.6182, 1988-12-31, 2006-12-31
  8, 11bd, 58ad0, 741.9156, 1988-12-31, 2006-12-31
  9, 1352, 17d9, 274.19855, 1988-12-31, 2006-12-31
  10, 13ba, 88356, 115.04227, 1988-12-31, 2006-12-31
  11, 1090, 3fb07, 767.21246, 1988-12-31, 2006-12-31
  12, 19c2, 8770b, 383.88382, 1988-12-31, 2006-12-31

Sections in This Chapter

Tables with Primary Key Column "GENERATED ... AS IDENTITY"

"INSERT INTO" Statements

"INSERT INTO" Statements with INDENTITY Columns

Handling Date and Timestamp Values

"UPDATE" Statements

"DELETE FROM" Statements

Dr. Herong Yang, updated in 2007
"INSERT INTO" Statements