您的位置:首页 > 数据库 > Oracle

How to get the rowid when insert the data to Oracle database

2008-08-20 11:59 621 查看
In my request, I need to get the rowid after insert the data to the oracle database. Oracle has an insert with returning clause, the gramer is:

INSERT INTO <table_name>

(column_list)

VALUES

(values_list)

RETURNING <value_name>

INTO <variable_name>;
How to get the rowid when insert the data to the database?

In JDBC, can use the CallbackStatement to run the Procedure, so we can generate the CallbackStatement from Connection object, and execute the insert sql, then get the return code from the statement Object. The key point is how to write the insert statement? and how to call the statement and how to get the return code. The following is my test code.

Create the test database

create an table FI_T_USER, which contain two columns, the first column is the primary key USER_ID, and the second column is USER_NAME. The create statement is as following:

create table FI_T_USER(

USER_ID varchar2(20) primary key,

USER_NAME varchar2(100)

);

Write the test code

The following is my test code:

package test.com.sinosoft.database;

import java.sql.*;

import oracle.jdbc.OracleTypes;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.sinosoft.database.DBConnectionPool;

import com.sinosoft.database.SqlQueryUtils;

import com.sinosoft.exception.SDBException;

The important code is specify the insertSQL, which is:

String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";

Then the key point is register the out parameter, and get the out parameter after execute the statement.

It's very useful, in some web site said that the this statement can only run start on Oracle 10, but in my tester database is Oracle 9.2, it can run the statement and get the return value.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: