![]() ![]() Using the MySQL-specific method call isn't portable, and issuing a SELECT to get the AUTOINCREMENT key's value requires another round-trip to the database, which isn't as efficient as possible. ![]() If you are creating gradle based project then use below build. With older JDBC drivers for MySQL, you could always use a MySQL-specific method on the Statement interface, or issue the query SELECT LASTINSERTID() after issuing an INSERT to a table that had an AUTOINCREMENT key. The name of the project is spring-jdbc-simplejdbcinsert-auto-generated-id. Get Auto-generated Id using SimpleJdbcInsert in Spring JDBCĮclipse 2020-06, At least JDK 1.8, Gradle 6.5.1, Maven 3.6.3, Spring Core/Context/Jdbc 5.2.8, MySQL 8.0.17 Project SetupĬreate either maven or gradle based project in Eclipse.So here I am not only going to show how to insert the auto generated Id value for the primary field, but also I am going to show you how to return it immediately after the record gets inserted into database table. How to get mysql auto increment key value using java jdbc, JDBC 3.0 introduced to get auto generated keys using getGeneratedKeys() method, It return the ResultSet object with the help of next. I am going to use PreparedStatement to get the auto generated id value.įor most of the records we use auto generated Id value on database side to avoid inconsistency while inserting value for the primary key Id field. Spring provides an easy way to get this auto-generated key using KeyHolder which is supported Spring JDBC 3 onward. With older JDBC drivers for MySQL, you could always use a MySQL-specific method on the Statement interface, or issue the query SELECT LASTINSERTID() after issuing an INSERT to a table that had an AUTOINCREMENT key. In this post I will show you how to get auto generated id of a newly inserted row in table.
0 Comments
Leave a Reply. |