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

来源:互联网 发布:时时彩源码 免费下载 编辑:程序博客网 时间:2024/05/20 13:15

 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呢?

<script type="text/javascript">google_ad_client = "pub-6924533005275861";google_ad_slot = "0030867594";google_ad_width = 300;google_ad_height = 250;</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
在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)
);

写测试代码

以下是我的测试代码:

 


 

  1. /*
  2.  * File name: TestInsertReturnRowId.java
  3.  * 
  4.  * Version: v1.0
  5.  * 
  6.  * 
  7.  */
  8. package test.com.sinosoft.database;
  9. import java.sql.*;
  10. import oracle.jdbc.OracleTypes;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import com.sinosoft.database.DBConnectionPool;
  15. import com.sinosoft.database.SqlQueryUtils;
  16. import com.sinosoft.exception.SDBException;
  17. /**
  18.  * 
  19.  * 
  20.  * 测试调用JDBC,往Oracle中插入数据,返回对应的ROWID
  21.  */
  22. public class TestInsertReturnRowId {
  23.     private static final Log log = LogFactory
  24.             .getLog(TestInsertReturnRowId.class);
  25.     public static void main(String[] args) {
  26.         TestInsertReturnRowId tester = new TestInsertReturnRowId();
  27.         String rowId = tester.insertUser("Stephen""liwp");
  28.         System.out.println("The rowId is:" + rowId);
  29.     }
  30.     public String insertUser(String userId, String userName) {
  31.         if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(userName)) {
  32.             log.error("Please specify the userId and userName");
  33.             return null;
  34.         }
  35.         // check whether the user has already in the database
  36.         String querySQL = "select count(1) as cnt from FI_T_USER where USER_ID = '"
  37.                 + userId + "'";
  38.         // insert statement
  39.         String insertSQL = "begin insert into FI_T_USER(USER_ID, USER_NAME) values(?,?) return rowid into ?;end;";
  40.         Connection con = DBConnectionPool.getConnection("test");
  41.         if (con == null) {
  42.             log.error("Error on get the connection!");
  43.             return null;
  44.         }
  45.         try {
  46.             int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
  47.             if (rowCount != 0) {
  48.                 log.error("User with userId = " + userId + " already exists!");
  49.                 return null;
  50.             }
  51.             // insert the data to the database
  52.             CallableStatement cs = con.prepareCall(insertSQL);
  53.             cs.setString(1, userId);
  54.             cs.setString(2, userName);
  55.             cs.registerOutParameter(3, OracleTypes.VARCHAR);
  56.             cs.execute();
  57.             String rowId = cs.getString(3);
  58.             return rowId;
  59.         } catch (SQLException e) {
  60.             e.printStackTrace();
  61.         } catch (SDBException e) {
  62.             e.printStackTrace();
  63.         } finally {
  64.             if (con != null) {
  65.                 try {
  66.                     con.close();
  67.                 } catch (SQLException e) {
  68.                     e.printStackTrace();
  69.                 }
  70.             }
  71.         }
  72.         return null;
  73.     }
  74. }

这里面很重要的代码是指定插入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,至于压缩嘛,就下次有机会再写了。

原创粉丝点击