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

"INSERT INTO" Statements with INDENTITY Columns

This section describes what will happen if you try to insert rows with values for INDENTITY columns.

An INDENTITY column is a special column in a table that defined to have it value automatically added with a sequence number generator. An INDENTITY column is normal used for the primary key column in a table. For example, the UserID column in the Profile table created in a previous tutorial is an INDENTITY column.

If you try to add values to an INDENTITY column in INSERT statements, you will get an error as shown in the sample program below:

/**
 * InsertIdentity.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class InsertIdentity {
  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"
        + " (UserID, FirstName, LastName, LastAccessDate)"
        + " VALUES (1001, 'Herong', 'Yang', '2007-04-01')");
      System.out.println("Number of rows inserted: "+count);

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

Here is the error message from this program:

SQLException: Cannot insert explicit value for identity column 
in table 'Profile' when IDENTITY_INSERT is set to OFF.

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 with INDENTITY Columns