在同一语句中获取插入的ID [英] Obtain id of an insert in the same statement

查看:85
本文介绍了在同一语句中获取插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在表中插入一行,并获得新生成的ID,只有一个语句?我想使用JDBC,并且ID将由序列生成或将是一个自动增量字段。

Is there any way of insert a row in a table and get the new generated ID, in only one statement? I want to use JDBC, and the ID will be generated by a sequence or will be an autoincrement field.

感谢您的帮助。

John Pollancre

John Pollancre

推荐答案

使用getGeneratedKeys():

using getGeneratedKeys():

resultSet = pstmt.getGeneratedKeys(); 

if (resultSet != null && resultSet.next()) { 
    lastId = resultSet.getInt(1); 
}

这篇关于在同一语句中获取插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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