繁体   English   中英

使用JDBC获取Oracle 11g的最后一个插入ID

[英]Get last insert id with Oracle 11g using JDBC

我刚开始使用Oracle,所以我将在之前回答过这个问题 我似乎无法让它工作。 这是我正在使用的声明:

declare
  lastId number;
begin
INSERT INTO "DB_OWNER"."FOO" 
  (ID, DEPARTMENT, BUSINESS)
  VALUES (FOO_ID_SEQ.NEXTVAL, 'Database Management', 'Oracle')
  RETURNING ID INTO lastId;
end;

当我调用executeQuery我所做的PreparedStatement时,它会将所有内容都插入到数据库中。 但是,我似乎无法弄清楚如何检索ID。 返回的ResultSet对象对我不起作用。 调用

if(resultSet.next()) ...

产生令人讨厌的SQLException,其内容如下:

无法对PLSQL语句执行提取:下一步

我怎么得到lastId 显然我做错了。

我尝试使用Oracle驱动程序v11.2.0.3.0(因为10.x和11.1.x中存在一些错误,请参阅其他博客 )。 以下代码工作正常:

final String sql = "insert into TABLE(SOME_COL, OTHER_COL) values (?, ?)";
PreparedStatement ps = con.prepareStatement(sql, new String[] {"ID"});
ps.setLong(1, 264);
ps.setLong(2, 1);
int executeUpdate = ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next() ) {
    // The generated id
    long id = rs.getLong(1);
    System.out.println("executeUpdate: " + executeUpdate + ", id: " + id);
}

使它成为一个函数,将它返回给你(而不是一个过程)。 或者,使用OUT参数的过程。

不确定这是否有效,因为我已经清除了所有Oracle的计算机,但......

将您的声明更改为:

declare
  lastId OUT number;

通过在连接上使用prepareCall()将语句从PreparedStatement切换到CallableStatement。 然后在调用之前注册输出参数,并在更新后读取它:

cstmt.registerOutParameter(1, java.sql.Types.NUMERIC);
cstmt.executeUpdate();
int x = cstmt.getInt(1);

准备语句时,将第二个参数设置为RETURN_GENERATED_KEYS 然后,您应该能够从语句对象中ResultSet

你是在存储过程中这样做的吗? 根据此Oracle文档 ,它不适用于服务器端驱动程序。

The Oracle server-side internal driver does not support 
the retrieval of auto-generated keys feature.

您可以使用Statement.getGeneratedKeys()来执行此操作。 您只需确保使用其中一个方法重载告诉JDBC您想要返回哪些列,例如此处的Connection.prepareStatement重载:

Connection conn = ...
PreparedStatement pS = conn.prepareStatement(sql, new String[]{"id"});
pS.executeUpdate();
ResultSet rS = pS.getGeneratedKeys();
if (rS.next()) {
  long id = rS.getLong("id");
  ...
}

你不需要用这个来做RETURNING x INTO东西,只需使用你想要的基本SQL语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM