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

Sections in This Chapter

"SELECT ... INTO" Statements

"INSERT INTO" Statements

"INSERT INTO" Statements with INDENTITY Columns

"UPDATE" Statements

"DELETE FROM" Statements

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