IBM DB2 Manual page 76

Table of Contents

Advertisement

import java.sql.*;
import java.math.*;
import com.ibm.db2.jcc.*;
Connection con;
Statement stmt;
ResultSet rs;
java.math.BigDecimal iDColVar;
...
stmt = con.createStatement();
stmt.executeUpdate(
stmt.executeUpdate("INSERT INTO EMP_PHONE (EMPNO, PHONENO) " +
rs = stmt.getGeneratedKeys();
while (rs.next()) {
}
rs.close();
stmt.close();
With any JDBC driver, you can retrieve the most recently assigned value of an
identity column using the IDENTITY_VAL_LOCAL() built-in function. Execute
code similar to this:
String idntVal;
Connection con;
Statement stmt;
ResultSet rs;
...
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1");
while (rs.next()) {
}
rs.close();
stmt.close();
Figure 19. Example of using IDENTITY_VAL_LOCAL() to return the most recent value of an
identity column
60
Application Programming Guide and Reference for Java
"CREATE TABLE EMP_PHONE (EMPNO CHAR(6), PHONENO CHAR(4), " +
"IDENTCOL INTEGER GENERATED ALWAYS AS IDENTITY)");
"VALUES ('000010', '5555')",
Statement.RETURN_GENERATED_KEYS);
java.math.BigDecimal idColVar = rs.getBigDecimal(1);
System.out.println("automatically generated key value = " + idColVar);
idntVal = rs.getString(1);
System.out.println("Identity column value = " + idntVal);
Related tasks
"Updating data in tables using the PreparedStatement.executeUpdate method"
on page 25
"Creating and modifying database objects using the Statement.executeUpdate
method" on page 24
// Create a Statement object
// Create table with identity column
// Insert a row
// Indicate you want automatically
// generated keys
// Retrieve the automatically
// generated key value in a ResultSet.
// Only one row is returned.
// Create ResultSet for query
// Get automatically generated key
// value
// Close ResultSet
// Close Statement
// Create a Statement object
// Get the result table from the query.
// This is a single row with the most
// recent identity column value.
// Position the cursor
// Retrieve column value
// Print the column value
// Close the ResultSet
// Close the Statement
1
2

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents