Oracle11g之实用技术将数据插入Oracle数据库时如何得到其rowId

Oracle11g之实用技术--将数据插入Oracle数据库时如何得到其rowId
Oracle11g有诸多的新特性,相信各位已经从很多渠道了解到了(注:还不清楚的请访问http://wmdata.com.cn/oracle/11g/index.asp?froms=blog),在此,我重点介绍一下如何在Oracle11g中插入数据时得到RowId,并公布一下,才发现的小秘密。

在有些应用场景下,我们需要在将数据插入到数据库时,返回rowId。Oracle有一条返回语句。其语法如下:


INSERT INTO <table_name>
(column_list)
VALUES
(values_list)
RETURNING <value_name>
INTO <variable_name>;

但在插入数据后,如何得到rowId呢?
在JDBC中,可以使用Callback语句去执行Procedure,因此,我们可以从连接对象产生Callback语句,并执行插入命令的SQL脚本,这样以从对象得到返回值。这个关键是如何写这条插入语句?并且如何去调用语句以及如何得到返回值。
 
以下是我的测试代码。

创建测试数据库

创建一个表FI_T_USER,这个表包含2字段,第一个是主键USER_ID,另一个是USER_NAME。创建语句如下:

create table FI_T_USER(
    USER_ID varchar2(20) primary key,
    USER_NAME varchar2(100)
);

写测试代码

以下是我的测试代码:

/*
 * File name: TestInsertReturnRowId.java
 * 
 * Version: v1.0
 * 
 * 
 
*/
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;

/**
 * 
 * 
 * 测试调用JDBC,往Oracle中插入数据,返回对应的ROWID
 
*/
public class TestInsertReturnRowId {
    
private static final Log log = LogFactory
            .getLog(TestInsertReturnRowId.
class);

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

    
public String insertUser(String userId, String userName) {
        
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");
        
if (con == null) {
            log.error(
"Error on get the connection!");
            
return null;
        }
        
try {
            
int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
            
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;
        } 
catch (SQLException e) {
            e.printStackTrace();
        } 
catch (SDBException e) {
            e.printStackTrace();
        } 
finally {
            
if (con != null) {
                
try {
                    con.close();
                } 
catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
return null;
    }
}


这里面很重要的代码是指定插入SQL脚本这句:

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

接下来的关键是,注册输出参数,并在执行语句后得到这个参数。

这些代码非常有用,不仅能在Oracle11g上使用,还可支持Oracle10和Oracle 9.2。


好了,本文开头所述,我发现的这个Oracle11g的秘密就是:
Oracle11g能在将数据导出备份时压缩数据,并且效率惊人。据Oracle11g白皮书中介绍,压缩率可达到74.67% , 本文主要介绍的是在Oracle11g中的实用技巧—插入数据时取得RowId,至于压缩嘛,就下次有机会再写了。

原文地址:https://www.cnblogs.com/meta/p/1294019.html