码迷,mamicode.com
首页 > 数据库 > 详细

C++完成Oracle存储过程批量插入(一)

时间:2014-09-02 00:26:13      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:c++   oracle批量插入   

为了满足大数据量的批量插入这个需求,最近研究了一下Oracle数据库的批量插入这块儿,本篇先介绍一下这两天来所了解到的以及一次不成功的C++进行存储过程批量插入方案。

一.Oracle处理批量插入存储过程

1.Oracle处理批量插入涉及到的相关概念有:Type、bulk collect、Oracle数组等。现在对它们依次简单的介绍一下。

1)Type

Type主要用于用户建立Oracle自定义类型。

Type常用的自定义类型介绍:

A.子类型

类似与全局定义的常量,只要改一处地方,与此常量相关的地方都修改了。一般子类型常用于货币类型的定义,如number(10,5),我们可以定义子类型:subtype hb_num is number(10,5),这样货币的精度需要修改时,不用搜遍全部地方,只需进行一处修改即可。

B.Object类型

定义了一个员工类

create or replace type type_employee as object(
id varchar2(36),
name varchar2(50),
age integer(3),
experience varchar2(4000)
);
可以使用该类型创建表,如下:

create table employee of type_employee
--则创建出了员工表
使用该Object类型,还可以创建其他类型,如下

--创建数组类型
create or replace type type_employee_arr as table of type_employee
更多详细的内容请看:Oracle Type介绍

2)oracle数组

A.固定数组

type v_arr is varray(10) of varchar2(50);

B.可变数组

type v_arr is table of varchar2(50);

C.多维数组

--下面的定义如同1)中的创建数组的情况,不过两者作用的范围不同
--以object对象建立的数组可用作任何地方
--以表定义的type不能定义全局的type,只可在函数、存储过程定义变量处定义
type v_table is table of poco_test%rowtype index by binary_integer;
3)bulk collect

bulk collect用于一次取出一个数据集合,比游标取数据效率高,但是其需要大量的内存,可以使用select into、fetch into、returning into语句到bulk collect中

declare
	type v_table is table of poco_test%rowtype index by binary_integer;
	v_values v_table;
begin
	select * bulk collect into v_values from poco_test;
end;
2.了解了相关的基本内容后,下面为本次研究Oracle批量插入的示例代码

create or replace type poco_test_object_type as
object( id varchar2(36),
name varchar2(200),code varchar(200));
create or replace type poco_test_object_arr_type
as table of poco_test_object_type;
create or replace procedure poco_test_arr_pro(v_arr in poco_test_object_arr_type) is
test_obj poco_test_object_type;
begin
  for idx in v_arr.first()..v_arr.last loop
    test_obj := v_arr(idx);
    insert into poco_test
    values(test_obj.id,test_obj.name,test_obj.code);
  end loop;
end poco_test_arr_pro;

declare
 abc poco_test_object_arr_type;
 o poco_test_object_type;
begin
 o:=poco_test_object_type(1,2,3);
 abc:=poco_test_object_arr_type(o,o,o);
 poco_test_arr_pro(abc);
end;

二、JDBC调用参数为数组类型的存储过程示例代码

public class PocoTestModel
{
	private String id;
	private String name;
	private String code;
	........
}
Connection con = null;  
CallableStatement cstmt = null;       
try {                 
	con = OracleConnection.getConn();  
	List<PocoTestModel> orderList = new ArrayList<PocoTestModel>();  
	for(int i=0;i<100000;i++){  
		orderList.add(new PocoTestModel(UUID.randomUUID().toString(),"name"+i,"code"+i));  
	}              
	StructDescriptor recDesc = StructDescriptor.createDescriptor("poco_test_object_type", con);  
	ArrayList<STRUCT> pstruct = new ArrayList<STRUCT>();  
	for (PocoTestModel ord:orderList) {                  
		Object[] record = new Object[3];  
		record[0] = ord.getId();  
		record[1] = ord.getName();  
		record[1] = ord.getCode();  
		STRUCT item = new STRUCT(recDesc, con, record);                  
		pstruct.add(item);  
	}             
	ArrayDescriptor tabDesc = ArrayDescriptor.createDescriptor("poco_test_object_arr_type", con);              
	ARRAY vArray = new ARRAY(tabDesc, con, pstruct.toArray());                     
	cstmt = con.prepareCall("{call poco_test_arr_pro(?)}");       
	cstmt.setArray(1, vArray);                   
	cstmt.execute();  
	con.commit();  
}catch ....
通过继承oracle.sql.ORAData接口,实现toDatum函数,可以简化执行的代码编写

public class PocoTestModel implements ORAData {
	private String id;
    private String name;
    private String code;
   
    public static final String _ORACLE_TYPE_NAME = "poco_test_object_type";   
	
    protected MutableStruct _struct;
    static int[] _sqlType = { OracleTypes.VARCHAR, OracleTypes.VARCHAR };
    static ORADataFactory[] _factory = new ORADataFactory[_sqlType.length];
	
    public PocoTestModel() {
        _struct = new MutableStruct(new Object[_sqlType.length], _sqlType, _factory);
    }
    public Datum toDatum(Connection conn) throws SQLException {
        _struct.setAttribute(0, this.id);
        _struct.setAttribute(1, this.name);
	_struct.setAttribute(1, this.code);
        return _struct.toDatum(conn, _ORACLE_TYPE_NAME);
    }
    public PocoTestModel(String id,String name, String code) {
        this();
        this.id = id;
        this.name = name;
	this.code = code;
    }
....
}

Connection con = null;
CallableStatement cstmt = null;     
try {     
	con = OracleConnection.getConn();           
	System.out.println(new Date());
	List<PocoTestModel> orderList = new ArrayList<PocoTestModel>();
	for(int i=0;i<100000;i++){
		orderList.add(new PocoTestModel(UUID.randomUUID().toString(),"name"+i,"code"+i));
	}        
	ArrayDescriptor tabDesc = ArrayDescriptor.createDescriptor("poco_test_object_arr_type", con);            
	ARRAY vArray = new ARRAY(tabDesc, con, orderList.toArray());       
	
	cstmt = con.prepareCall("{call poco_test_arr_pro(?)}");     
	cstmt.setArray(1, vArray);               
	cstmt.execute();
	con.commit();
}catch ...

上面是所查到的Oracle批量插入的资料,关于C++方面,没有找到像JDBC类似的方法,提供数组类型的参数的存储过程的调用。下篇文章将介绍另外一个方案,来完成Oracle数据的批量插入。





C++完成Oracle存储过程批量插入(一)

标签:c++   oracle批量插入   

原文地址:http://blog.csdn.net/fengshuiyue/article/details/38959925

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!