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,至于压缩嘛,就下次有机会再写了。

posted on   公众号73只蚂蚁  阅读(2585)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2008年9月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示