插入表格后返回ID的自动递增值 [英] Returning the autoincremented value of the id after insert into a table

查看:81
本文介绍了插入表格后返回ID的自动递增值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用密钥持有者来获取插入时自动增加的ID.在实际表上工作正常.

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

我在另一个数据库中有一个同义词表,该数据库实际上指向原始表,因此我必须在同义词表中进行插入.当我尝试在同义词表中进行插入时,使用的方法为keyholder.getkey()返回0.请帮助我.

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..

参考: https://stackoverflow.com/questions/241003/如何从最后插入的行中获取值 http://docs. spring.io/spring/docs/2.5.x/reference/jdbc.html#jdbc-auto-genereted-keys

添加:

我尝试过

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

但这也返回0.为什么这在同义词表中不起作用?我的意思是,它将插入自动递增的值,但返回"0".

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'.

推荐答案

对于具有相同问题的任何人,我都使用了一个名为getSerial()的函数来获取序列号. 感谢您的退房!

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

这篇关于插入表格后返回ID的自动递增值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆