how to find the last insert id in java+mysql?


JDBC 3.0 (MySQL has a JDBC 3 compliant driver) method as such:

Statement stat = dbConn.createStatement();
Stat.executeUpdate("Some insert statement here");

ResultSet rs = stat.getGeneratedKeys();
Int insertedKeyValue = rs.getInt(1);

Rs.close();
Stat.close();

Related Posts:

One Responses


ronnil January 23, 2009 at 1:32 pm

if this doesn’t work, try putting a “rs.next()” before the getInt() statement, that should do the trick.


Comments are closed.