1 首先我已經在oracle 數據庫建立了自定義對象Type Object 和 自定義表 Type table 和procedurejava
如下爲相關ddlsql
create or replace TYPE ASU_SAVE_OBJ AS OBJECT ( ASUCODE VARCHAR2(8 BYTE) , ASU_TITLE VARCHAR2(60 BYTE), GEOGRAPHICAL_DEF VARCHAR2(1000 BYTE), STATECODE VARCHAR2(2 BYTE), PROGYEAR NUMBER(4,0) ); create or replace TYPE T_ASU_SAVE_OBJ AS TABLE OF ASU_SAVE_OBJ; create or replace PROCEDURE ASU_BATCH_SAVE ( list_asu IN T_ASU_SAVE_OBJ ) AS BEGIN for i in list_asu.first .. list_asu.last loop insert into asu (ASUCODE, ASU_TITLE, geographical_def,statecode,progyear) VALUES (list_asu(i).asucode, list_asu(i).ASU_TITLE, list_asu(i).geographical_def, list_asu(i).statecode, list_asu(i).progyear); --dbms_output.put_line(list_asu(i).asucode || list_asu(i).ASU_TITLE || list_asu(i).geographical_def || list_asu(i).statecode || list_asu(i).progyear); end loop; commit; EXCEPTION WHEN OTHERS THEN rollback; END ASU_BATCH_SAVE;
2. 在瞭解如何 把 java collection 經過jdbc 轉爲 db array/table(db collection) 以前數據庫
首先知道如何把 java object 參數轉換爲db objectoracle
一個db object 須要使用Object[] 來表示, object[] 下的每一個元素表示db object 的一個屬性ide
參考上面對應ASU_SAVE_OBJoop
java object[] 對應的db object 以下this
代碼debug
Object[] bean = new Object[]{asuCode,asuTitle,asuPgd,this.stateCode,Integer.parseInt(progYear)};
而後把java collection 轉換爲 db collectioncode
代碼對象
private List<Object> convert2DBObj(String asuCode, String stateCode, String progYear, String selCompAreas){ String[] compArr=selCompAreas.split(" "); List<Object> result=new ArrayList<Object>(); for(String compArea: compArr){ if(compArea.startsWith("+")){ logger.debug("List["+ stateCode+","+asuCode+","+progYear+","+compArea+",+]"); result.add(new Object[] {stateCode,asuCode,progYear,compArea.substring(1),"+" }); } else{ logger.debug("List["+ stateCode+","+asuCode+","+progYear+","+compArea+",-]"); result.add(new Object[] {stateCode,asuCode,progYear,compArea.substring(1),"-" }); } } return result; }
3 最後使用JDBC API 經過oracle.array 和 ArrayDescriptor 轉換java list 爲array
其中我已經在oracle 數據庫建立了一個叫ASU_BATCH_SAVE(?) procedure
(1)這裏解釋一下ArrayDescriptor, ArrayDescriptor 主要用來綁定這個procedure的輸入參數類型是T_ASU_SAVE_OBJ, 注意這裏我已經在數據庫建立了
CREATE OR REPLACE TYPE T_ASU_SAVE_OBJ
AS TABLE OF ASU_SAVE_OBJ; 這樣一個輸入參數
(2) ARRAY 須要三個參數一個是ArrayDescriptor ,一個是connection,最後一個書list<object> 可是這裏必須轉換爲最終形式Object[] arrayData = listASU.toArray();
由於只接收Object[]
代碼:
dataSource = ServiceLocator.getInstance().getDataSource(vProcessMode); dbConnection = DAOUtility.openConnection(dataSource, this.autoCommit); String sql = "{call ASU_BATCH_SAVE(?)} "; cst = dbConnection.prepareCall(sql); Object[] arrayData = listASU.toArray(); ArrayDescriptor descriptor = new ArrayDescriptor("T_ASU_SAVE_OBJ", dbConnection); ARRAY t_list = new ARRAY(descriptor,dbConnection,arrayData); cst.setArray(1, t_list); cst.execute();