"INSERT INTO" - Inserting New Data Rows

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

INSERT statements are used very often by database applications to insert new 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:

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

/* OracleMultipleInserts.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
import java.sql.*;
public class OracleMultipleInserts {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Setting up the DataSource object
      oracle.jdbc.pool.OracleDataSource ds 
        = new oracle.jdbc.pool.OracleDataSource();
      ds.setDriverType("thin");
      ds.setServerName("localhost");
      ds.setPortNumber(1521);
      ds.setDatabaseName("XE"); // Oracle SID
      ds.setUser("Herong");
      ds.setPassword("TopSecret");

// Getting a connection object and statement object
      con = ds.getConnection();
      Statement sta = con.createStatement(); 
      int count = 0;

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

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

// 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"
          + " (ID, FirstName, LastName, Point)"
          + " VALUES ("+(i+10)+", '"+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.getDouble("Point")
           + ", "+res.getDate("BirthDate")
           + ", "+res.getTimestamp("ModTime"));
      }
      res.close();

      sta.close();
      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\java 
   -cp .;\local\lib\ojdbc6.jar OracleMultipleInserts

Number of rows inserted: 12
List of Profiles:
  10, 17cb, 56a65, 559.5103, 1988-12-31, 2006-12-31 11:59:59.999
  1, Herong, null, 0.0, 1988-12-31, 2006-12-31 11:59:59.999
  2, Janet, Gates, 999.99, 1984-10-13, 2006-12-31 11:59:59.999
  11, 218, ed1e7, 727.6421, 1988-12-31, 2006-12-31 11:59:59.999
  12, bd, 346ac, 214.58476, 1988-12-31, 2006-12-31 11:59:59.999
  13, 19fa, c4280, 852.094, 1988-12-31, 2006-12-31 11:59:59.999
  14, 25c, 9fb63, 860.36957, 1988-12-31, 2006-12-31 11:59:59.999
  15, 1f85, 924f4, 125.26178, 1988-12-31, 2006-12-31 11:59:59.999
  16, 47b, 3c5d9, 882.1738, 1988-12-31, 2006-12-31 11:59:59.999
  17, 196e, 71d6e, 981.2028, 1988-12-31, 2006-12-31 11:59:59.999
  18, f4c, 4bdfe, 368.7445, 1988-12-31, 2006-12-31 11:59:59.999
  19, 2d4, 871a4, 433.00528, 1988-12-31, 2006-12-31 11:59:59.999

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

Oracle JDBC Drivers

 Oracle JDBC Drivers Overview

 JDBC Thin Client-Side Driver Installation

 Loading JDBC Driver Class - ojdbc16.jar

 JDBC Driver Connection URL

 Creating Connections with DataSource Class

 DataSource Error - makeURL() Failed

 Getting Driver and Server Information

 "CREATE TABLE" - Creating New Tables

"INSERT INTO" - Inserting New Data Rows

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version