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 a single row into my table Herong.Profile:

/**
 * InsertRows.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class InsertRows {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");
      Statement sta = con.createStatement(); 

// insert a single row
      int count = sta.executeUpdate(
        "INSERT INTO Herong.Profile"
        + " (FirstName, LastName, LastAccessDate)"
        + " VALUES ('Herong', 'Yang', '2007-04-01')");
      System.out.println("Number of rows inserted: "+count);

// getting the data back
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Herong.Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("UserID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getDate("LastAccessDate"));
      }
      res.close();

      sta.close();
      con.close();        
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

The output confirms that the insert statement was executed correctly:

Number of rows inserted: 1
List of Profiles:
  13, Herong, Yang, 2007-04-01
  1, Orlando, Gee, 2004-10-13
  2, Keith, Harris, 2004-10-13
  3, Donna, Carreras, 2004-10-13
  4, Janet, Gates, 2004-10-13
  5, Lucy, Harrington, 2004-10-13
  6, Rosmarie, Carroll, 2004-10-13
  7, Dominic, Gash, 2004-10-13
  10, Kathleen, Garza, 2004-10-13
  11, Katherine, Harding, 2004-10-13
  12, Johnny, Caprio, 2004-10-13

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 Downloading and Installing JDK - Java SE

 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 - 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 - sqljdbc.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

Microsoft JDBC Driver - DML Statements

 "SELECT ... INTO" Statements

"INSERT INTO" Statements

 "INSERT INTO" Statements with INDENTITY Columns

 "UPDATE" Statements

 "DELETE FROM" 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

 References

 PDF Printing Version

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