Returning the autoincremented value of the id after insert into a table

左心房为你撑大大i 提交于 2019-12-12 01:51:49

问题


I am using keyholder to get the id that is automatically incremented while inserting. It works fine on the actual table.

I have a synonym table in another database which actually points to the original table and I have to do the insert into the synonym table. The method used is returning 0 for keyholder.getkey() when I try to do the insert in the synonym table. Please help me..

References: https://stackoverflow.com/questions/241003/how-to-get-a-value-from-the-last-inserted-row http://docs.spring.io/spring/docs/2.5.x/reference/jdbc.html#jdbc-auto-genereted-keys

ADD:

I have tried

 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);
}

But this is returning 0 too. Why is this not working in a synonym table? I mean it inserts the auto incremented value but return '0'.


回答1:


For anyone with the same issue, I have used a function called getSerial() to get the serial. Thanks for checking out!



来源:https://stackoverflow.com/questions/28465380/returning-the-autoincremented-value-of-the-id-after-insert-into-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!