This section describes how to insert rows to the test table with an IDENTITY column.
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 inserts some rows into my table Profile:
/**
* SqlServerMultipleInserts.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.util.*;
import java.sql.*;
public class SqlServerMultipleInserts {
public static void main(String [] args) {
Connection con = null;
try {
// Setting up the DataSource object
com.microsoft.sqlserver.jdbc.SQLServerDataSource ds
= new com.microsoft.sqlserver.jdbc.SQLServerDataSource();
ds.setServerName("localhost");
ds.setPortNumber(1269);
ds.setDatabaseName("AdventureWorksLT");
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"
+ " (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.getDouble("Point")
+ ", "+res.getDate("BirthDate")
+ ", "+res.getTimestamp("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 insert statements was executed correctly: