1. 程式人生 > >Oracle11g之實用技術 將資料插入Oracle資料庫時如何得到其rowId

Oracle11g之實用技術 將資料插入Oracle資料庫時如何得到其rowId

               

 Oracle11g之實用技術--將資料插入Oracle資料庫時如何得到其rowIdOracle11g有諸多的新特性,相信各位已經從很多渠道瞭解到了(注:還不清楚的請訪問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"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. publicclass TestInsertReturnRowId {
  23. privatestaticfinal Log log = LogFactory
  24.             .getLog(TestInsertReturnRowId.class);
  25. publicstaticvoid 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. returnnull;
  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. returnnull;
  44.         }
  45. try {
  46. int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
  47. if (rowCount != 0) {
  48.                 log.error("User with userId = " + userId + " already exists!");
  49. returnnull;
  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. returnnull;
  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,至於壓縮嘛,就下次有機會再寫了。