Retrieving IDs of inserted records using mySQL with mm.mysql JDBC driver is a pretty common requirement, assuming you’re not generating your own IDs. JDBC 3.0 provides a standard means of retrieving the inserted ID, but for prior code, here’s how to do it.

Retrieving IDs of Updated Rows with AUTO_INCREMENT Fields

Cast the java.sql.Statement that is created from the Connection.createStatement() call and cast it to org.gjt.mm.mysql.Statement.

You should then be able to call the getLastInsertID() method on the Statement to get the insert ID of the last row inserted for the Statement.

I’ve heard some unconfirmed reports of unreliable behavior in the older drivers, but I’ve never had problems with this.

Related posts:

  1. Converting from MS Access to mySQL