Retrieving identity column values in JDBC applications:
You define an identity column in a CREATE TABLE by specifying the IDENTITY clause when you define a column.
This feature is defined in the JDBC, and implmented in the MS Sql Server 2005, not in the older version. For Oracle database this is not implemented, but the common solution of the rownum can be used instead.
1.Set the prepared statement to get the identity column from DB:
Connection.prepareStatement(sql-statement,
Statement.RETURN_GENERATED_KEYS);
or
Statement.executeUpdate(sql-statement, Statement.RETURN_GENERATED_KEYS);
2. Get the generated values from the statement
rs = stmt.getGeneratedKeys();
while (rs.next()) {
rs.getBigDecimal(1);
}
Technorati Tags: ms sql server, jdbc, identity
BTW, it seems like MSSQL driver doesn’t support
keys.getInt(“ID”) but supports keys.getInt(1)
while the JTDS driver (1.2) supports both.