插入行jdbc的主键? [英] Primary key from inserted row jdbc?

查看:112
本文介绍了插入行jdbc的主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有跨数据库平台方式来获取刚刚插入的记录的主键?

Is there a cross database platform way to get the primary key of the record you have just inserted?

我注意到这个答案说你可以通过调用 SELECT LAST_INSERT_ID( )我想你可以调用 SELECT @@ IDENTITY AS'Identity'; 有没有一种常见的方法在jdbc中跨数据库做这个?

I noted that this answer says that you can get it by Calling SELECT LAST_INSERT_ID() and I think that you can call SELECT @@IDENTITY AS 'Identity'; is there a common way to do this accross databases in jdbc?

如果不是,您会如何建议我为可以访问任何SQL Server,MySQL和Oracle的代码实现此功能?

If not how would you suggest I implement this for a piece of code that could access any of SQL Server, MySQL and Oracle?

推荐答案

从我的代码复制:

pInsertOid = connection.prepareStatement(INSERT_OID_SQL, Statement.RETURN_GENERATED_KEYS);

其中pInsertOid是预备声明。

where pInsertOid is a prepared statement.

然后你可以获得密钥:

// fill in the prepared statement and
pInsertOid.executeUpdate();
ResultSet rs = pInsertOid.getGeneratedKeys();
if (rs.next()) {
  int newId = rs.getInt(1);
  oid.setId(newId);
}

希望这能给你一个很好的起点。

Hope this gives you a good starting point.

这篇关于插入行jdbc的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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