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));
寫測試程式碼
以下是我的測試程式碼:
- /*
- * 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
- import org.apache.commons.logging.LogFactory;
- import com.sinosoft.database.DBConnectionPool;
- import com.sinosoft.database.SqlQueryUtils;
- import com.sinosoft.exception.SDBException;
- /**
- *
- *
- * 測試呼叫JDBC,往Oracle中插入資料,返回對應的ROWID
- */
- publicclass TestInsertReturnRowId {
- privatestaticfinal Log log = LogFactory
- .getLog(TestInsertReturnRowId.class);
- publicstaticvoid 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");
- returnnull;
- }
- // 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!");
- returnnull;
- }
- try {
- int rowCount = SqlQueryUtils.getIntValue(querySQL, con);
- if (rowCount != 0) {
- log.error("User with userId = " + userId + " already exists!");
- returnnull;
- }
- // 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();
- }
- }
- }
- returnnull;
- }
- }
這裡面很重要的程式碼是指定插入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,至於壓縮嘛,就下次有機會再寫了。