Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve the autoincrement ID from a prepared statement

Is there a way to retrieve the auto generated key from a DB query when using a java query with prepared statements.

For example, I know AutoGeneratedKeys can work as follows.

stmt = conn.createStatement();  stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); if(returnLastInsertId) {     ResultSet rs = stmt.getGeneratedKeys();     rs.next();     auto_id = rs.getInt(1); }  

However. What if I want to do an insert with a prepared Statement.

String sql = "INSERT INTO table (column1, column2) values(?, ?)"; stmt = conn.prepareStatement(sql);  //this is an error stmt.executeUpdate(Statement.RETURN_GENERATED_KEYS); if(returnLastInsertId) {     //this is an error since the above is an error     ResultSet rs = stmt.getGeneratedKeys();     rs.next();     auto_id = rs.getInt(1); }  

Is there a way to do this that I don't know about. It seems from the javadoc that PreparedStatements can't return the Auto Generated ID.

like image 584
jW. Avatar asked Sep 03 '09 22:09

jW.


People also ask

How can I get auto increment ID?

To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment. Inserting records into the table. To display all the records.

How can I get auto increment ID in SQL Server?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

What is the function used to get the value of recently inserted ID in an autoincrement column?

To obtain the value immediately after an INSERT , use a SELECT query with the LAST_INSERT_ID() function. For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.

What will be the auto increment ID of delete?

It is often the case that a column such as the ID column on a table will auto increment. This simply means that the next insert into the table will have an ID that is one more then the previous one and therefore all ID's will be unique.


2 Answers

Yes. See here. Section 7.1.9. Change your code to:

String sql = "INSERT INTO table (column1, column2) values(?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);   stmt.executeUpdate(); if(returnLastInsertId) {    ResultSet rs = stmt.getGeneratedKeys();     rs.next();    auto_id = rs.getInt(1); } 
like image 70
Yishai Avatar answered Sep 22 '22 21:09

Yishai


There's a couple of ways, and it seems different jdbc drivers handles things a bit different, or not at all in some cases(some will only give you autogenerated primary keys, not other columns) but the basic forms are

stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);  

Or use this form:

String autogenColumns[] = {"column1","column2"}; stmt = conn.prepareStatement(sql, autogenColumns) 
like image 26
nos Avatar answered Sep 21 '22 21:09

nos