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

本文介绍如何使用JDBC在Oracle数据库中插入数据后获取ROWID。通过定义特定的插入语句并利用CallableStatement来执行存储过程,可以实现这一目标。文章提供了一个具体的Java示例程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     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(20primary key
    
USER_NAME varchar2(100)
);

 Write the test code

     The following is my test code:

 

ExpandedBlockStart.gifContractedBlock.gif/**//*
 * File name: TestInsertReturnRowId.java
 * 
 * Version: v1.0
 * 
 * Created on Aug 20, 2008 8:56:07 AM
 * 
 * Designed by Stephen
 * 
 * (c)Copyright 2008
 
*/

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;

ExpandedBlockStart.gifContractedBlock.gif
/** *//**
 * 
@author Stephen
 * 
 * 测试调用JDBC,往Oracle中插入数据,返回对应的ROWID
 
*/

ExpandedBlockStart.gifContractedBlock.gif
public class TestInsertReturnRowId {
    
private static final Log log = LogFactory
            .getLog(TestInsertReturnRowId.
class);

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) {
        TestInsertReturnRowId tester 
= new TestInsertReturnRowId();
        String rowId 
= tester.insertUser("Stephen""liwp");
        System.out.println(
"The rowId is:" + rowId);
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String insertUser(String userId, String userName) {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userName)) {
            log.error(
"Please specify the userId and userName");
            
return null;
        }

        
// check whether the user has already in the database
        String querySQL = "select count(1) as cnt from FI_T_USER where USER_ID = '"
                
+ userId + "'";
        
// insert statement
        String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
        Connection con 
= DBConnectionPool.getConnection("test");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (con == null{
            log.error(
"Error on get the connection!");
            
return null;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (rowCount != 0{
                log.error(
"User with userId = " + userId + " already exists!");
                
return null;
            }

            
// insert the data to the database
            CallableStatement cs = con.prepareCall(insertSQL);
            cs.setString(
1, userId);
            cs.setString(
2, userName);
            cs.registerOutParameter(
3, OracleTypes.VARCHAR);
            cs.execute();
            String rowId 
= cs.getString(3);
            
return rowId;
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SDBException e) {
            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (con != null{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try {
                    con.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 catch (SQLException e) {
                    e.printStackTrace();
                }

            }

        }

        
return null;
    }

}

     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.

转载于:https://www.cnblogs.com/liwp_Stephen/archive/2008/08/20/1271925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值