使用JDBC获取Oracle 11g的最后一个插入ID [英] Get last insert id with Oracle 11g using JDBC

查看:94
本文介绍了使用JDBC获取Oracle 11g的最后一个插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Oracle的新手,所以我不再使用

I'm new to using Oracle so I'm going off what has already been previously answered in this SO question. I just can't seem to get it to work. Here's the statement that I'm using:

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对象对我不起作用.打电话

When I call executeQuery the PreparedStatement that I have made, it inserts everything into the database just fine. However, I cannot seem to figure out how to retrieve the ID. The returned ResultSet object will not work for me. Calling

if(resultSet.next()) ...

产生一个讨厌的SQLException,内容为:

yields a nasty SQLException that reads:

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

Cannot perform fetch on a PLSQL statement: next

我怎么得到那个lastId?显然我做错了.

How do I get that lastId? Obviously I'm doing it wrong.

推荐答案

使其成为一个将其返回给您的函数(而不是过程).或者,使用带有OUT参数的过程.

make it a function that returns it to you (instead of a procedure). Or, have a procedure with an OUT parameter.

这篇关于使用JDBC获取Oracle 11g的最后一个插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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