How to Get Last Inserted Record ID in MySQL Using Java (JDBC)

When we insert a record in a table that contains an auto increment column then we can easily obtain its id (primary key).

Below example will show you how you can get the id of last inserted record in MySQL using Java (JDBC). In my example the table has following structure. In this case id column is auto increment primary key.

How to Get Last Inserted Record ID in MySQL Using Java

 

How to Get Last Inserted Record ID in MySQL Using Java (JDBC)

 

Output:

id: 3

 

If you have any doubts regarding above code then you can ask it by commenting below.

3 thoughts on “How to Get Last Inserted Record ID in MySQL Using Java (JDBC)”

Leave a Comment

Your email address will not be published. Required fields are marked *