问题
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