PPAS上以自定义类型数组为入参的函数及调用加java调用
--1
--创建表
CREATE TABLE TABLE_TEST
(
ID NUMBER(10,0) NOT NULL,
NAME VARCHAR2 NOT NULL,
ATTRIBUTE1 VARCHAR2,
ATTRIBUTE2 VARCHAR2,
CONSTRAINT PK_TABLE_TEST PRIMARY KEY (ID)
);
--2. 创建序列 s_TABLE_TEST.
create sequence s_TABLE_TEST start 1;
--3
--创建类型
create or replace type T_TYPE is object (
NAME VARCHAR2(100),
ATTRIBUTE1 VARCHAR2(100),
ATTRIBUTE2 VARCHAR2(100)
);
--4
--因为不支持组合类型做为出、入参数,改为如下:
create or replace procedure POPULATE_TABLE_TEST (example T_TYPE[]) as
type t_tb_seq is table of number;
l_t_seq t_tb_seq := t_tb_seq();
begin
for i in array_lower(example,1) .. array_upper(example,1) loop
l_t_seq.extend;
select s_TABLE_TEST.nextval into l_t_seq(i) from dual;
end loop;
for i in array_lower(example,1) .. array_upper(example,1) loop
insert into TABLE_TEST(id,name,attribute1,attribute2)
values (
l_t_seq(i),
example[i].NAME,
example[i].ATTRIBUTE1,
example[i].ATTRIBUTE2
);
end loop;
end;
--5
--因为不支持结构,创建T_TYPE类并覆盖其toString()方法
class T_Type {
private String name, attribute1, attribute2;
T_Type(String name, String attribute1, String attribute2) {
this.name = name;
this.attribute1 = attribute1;
this.attribute2 = attribute2;
}
@Override
public String toString() {
return "(\"" + name +"\","
+ " \"" + attribute1 + "\","
+ " \"" + attribute2 + "\")";
}
}
--6
--调用存储过程
T_Type[] values = new T_Type[]{new T_Type("obj1_name","obj1_att1", "obj1_att2"),
new T_Type("obj2_name","obj2_att1", "obj2_att2"),
new T_Type("obj3_name","obj3_att1", "obj3_att2")};
CallableStatement cstmt = con.prepareCall("{call POPULATE_TABLE_TEST(?)}");
Array array = con.createArrayOf("t_type", values);
cstmt.setArray(1, array);
cstmt.execute();
--注意
--为了使用方法con.createArrayOf,应该使用JDK1.6+和edb-jdbc16.jar,因为createArrayOf是JDBC4的功能。