Java JDBC Get Last Inserted ID

If you don’t like stored procedures wherein the value returned is the last inserted id, you can get it via JDBC code. Using either a Statement or PreparedStatement class, you retrieve the ResultSet using the getGeneratedKeys() method. Sample code below.

ResultSet rskey = ps.getGeneratedKeys();
 
if (rskey != null && rskey.next()) {
  int lastInsertedId = rskey.getInt(1);
}

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails