// Insert one row that will generate an AUTO INCREMENT
// key in the primary key field
//
stmt.executeUpdate(
"INSERT INTO autoIncTest (comment) "
+ "values ('How can I get the auto increment field value?')",
Statement.RETURN_GENERATED_KEYS);
//
// Use Statement.getGeneratedKeys()
// to retrieve the value(s)
//
int autoIncValue = -1;
rs = stmt.getGeneratedKeys();
if (rs.next()) {
autoIncValue = rs.getInt(1);
} else {
// Error
}
rs.close();
資料來源:
http://blog.taragana.com/index.php/archive/java-how-to-get-auto-increment-values-after-sql-insert/
沒有留言:
張貼留言