jdbc批量(bulk )insert———oracle數組類型與forall的應用

該貼轉自如下頁面,原標題爲“oracle bulk Insert 比insert 快不是一點啊.

http://blog.itpub.net/post/37572/465011 

發表人:jidongzheng | 發表時間: 2008年六月25日, 11:53

 

測試java的insert 同bulk Insert (9i以上) 的速度.
測試結果: bulk Insert 速度相當的快.
100000條記錄
insert ,---------------93秒
bulk insert -------------0.441秒

環境:
oracle 10.2.0.3 Windows 2000Server

SQL> desc a
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
ID INTEGER Y
NAME VARCHAR2(20) Y

 

bulk Insert 使用的類型及過程:
create or replace type i_table is table of number(10);
create or replace type v_table is table of varchar2(10);

create or replace procedure pro_forall_insert(v_1 i_table,v_2 v_table)
as
c integer;
begin
forall i in 1.. v_1.count
insert into a values(v_1(i),v_2(i));
end;


測試的java代碼:

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;
import oracle.jdbc.OracleTypes;
import oracle.sql.*;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
import java.sql.Connection;
import java.sql.DriverManager;
import oracle.jdbc.OracleCallableStatement;


public class testOracle {
public testOracle() {

Connection oraCon = null;
PreparedStatement ps = null;
Statement st = null;
ResultSet rs = null;
try {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException ex) {}
oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.234:1521:ora10g", "imcs","imcs");
oraCon.setAutoCommit(false);
} catch (SQLException ex) {
ex.printStackTrace();
}

CallableStatement cstmt = null;
oracle.sql.ArrayDescriptor a = null;
oracle.sql.ArrayDescriptor b = null;

Object[] s1 = new Object[100000];
Object[] s2 = new Object[100000];

for (int i = 0; i < 100000; i++) {
s1[i] = new Integer(1);
s2[i] = new String("aaa").concat(String.valueOf(i));
}
try {
 
a = oracle.sql.ArrayDescriptor.createDescriptor("I_TABLE", oraCon);
b = oracle.sql.ArrayDescriptor.createDescriptor("V_TABLE", oraCon);
ARRAY a_test = new ARRAY(a, oraCon, s1);
ARRAY b_test = new ARRAY(b, oraCon, s2);
cstmt = oraCon.prepareCall("{ call pro_forall_insert(?,?) }");
cstmt.setObject(1, a_test);
cstmt.setObject(2, b_test);

long aaaa = System.currentTimeMillis();
cstmt.execute();
oraCon.commit();
System.out.println(System.currentTimeMillis()-aaaa);
} catch (Exception e) {
e.printStackTrace();
}


try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}


public static void main(String args[]) {
testOracle a = new testOracle();
}

private void jbInit() throws Exception {
}


};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章