oracle bulk load

First create types in oracle
DROP TYPE CINBELLINP.D_TABLE;

CREATE OR REPLACE TYPE CINBELLINP.d_table is table of Date;
/

DROP TYPE CINBELLINP.I_TABLE;

CREATE OR REPLACE TYPE CINBELLINP.i_table is table of number(10);
/

DROP TYPE CINBELLINP.V_TABLE;

CREATE OR REPLACE TYPE CINBELLINP.v_table is table of varchar2(10);
/


second, create procedure

CREATE OR REPLACE procedure CINBELLINP.pro_forall_insert(v_1 i_table,v_2 v_table, v_3 d_table)
as
begin

forall i in 1.. v_1.count
insert into a values(v_1(i),v_2(i), v_3(i));
end;
/


in java side call
package com.whatever

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;

import oracle.sql.ARRAY;


public class testOracle {
	public testOracle() {
		
		Connection oraCon = null;
		try {
			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} 
			catch (ClassNotFoundException ex)
				{}
			oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.1:1521:Test", "test","test");
			oraCon.setAutoCommit(false);
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		System.out.println("now");
		
		CallableStatement cstmt = null;
		oracle.sql.ArrayDescriptor a = null;
		oracle.sql.ArrayDescriptor b = null;
		oracle.sql.ArrayDescriptor c = null;
		java.util.Date datetime=new java.util.Date();  
		java.sql.Date mySqlDate = new java.sql.Date(datetime.getTime());
		if (true )
		{
			Object[] s1 = new Object[1000000];
			Object[] s2 = new Object[1000000];
			Object[] s3 = new Object[1000000];
			
			for (int i = 0; i < 1000000; i++) {
			s1[i] = new Integer(1);
			s2[i] = new String("aaa").concat(String.valueOf(i));
			s3[i] = mySqlDate;
			}
			try {
				a = oracle.sql.ArrayDescriptor.createDescriptor("I_TABLE", oraCon);
				b = oracle.sql.ArrayDescriptor.createDescriptor("V_TABLE", oraCon);
				c = oracle.sql.ArrayDescriptor.createDescriptor("D_TABLE", oraCon);
				ARRAY a_test = new ARRAY(a, oraCon, s1);
				ARRAY b_test = new ARRAY(b, oraCon, s2);
				ARRAY c_test = new ARRAY(c, oraCon, s3);
				cstmt = oraCon.prepareCall("{ call pro_forall_insert(?,?,?) }");
				cstmt.setObject(1, a_test);
				cstmt.setObject(2, b_test);
				cstmt.setObject(3, c_test);
				long aaaa = System.currentTimeMillis();
				System.out.println(System.currentTimeMillis());
				cstmt.execute();
				oraCon.commit();
				System.out.println("now");
				System.out.println(System.currentTimeMillis()-aaaa);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		try{
				PreparedStatement oraPs = null;
				String oraInsertSql =
				"insert into a values(?,?,?)";
				
				oraPs = oraCon.prepareStatement(oraInsertSql);
				long aaaa = System.currentTimeMillis();
				System.out.println(System.currentTimeMillis());
				datetime=new java.util.Date();  
				mySqlDate = new java.sql.Date(datetime.getTime());
				for (int i = 0; i < 1000000; i++)
				{
					oraPs.setInt(1,i);
					oraPs.setString(2, new String("aaa").concat(String.valueOf(i)));
					oraPs.setDate(3, mySqlDate);
					oraPs.executeUpdate();
				}
				oraCon.commit();
				System.out.println(System.currentTimeMillis()-aaaa);
				}
				catch (SQLException ex)
				{
					System.out.print("dddddd");
					System.out.print(ex.getMessage());
				}
		try {
			System.out.println("finished");
			jbInit();
		} 
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	public static void main(String args[]) {
	testOracle a = new testOracle();
	}
	
	private void jbInit() throws Exception {
	}


}

猜你喜欢

转载自intrepid2012.iteye.com/blog/1323533