17,377
社区成员
发帖
与我相关
我的任务
分享
CREATE OR REPLACE TYPE SQLS AS TABLE OF VARCHAR2(2000);
CREATE OR REPLACE PROCEDURE PRO(SQL_LIST IN SQLS)
AS
BEGIN
FOR I IN 0..SQL_LIST.COUNT LOOP
EXECUTE IMMEDIATE SQL_LIST(I);
END LOOP;
END;
/
package snt.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import snt.test.GetTime;
public class JDBCTest {
static String driver="oracle.jdbc.driver.OracleDriver";
static String url="jdbc:oracle:thin:@localhost:1521:orcl";
public static void main(String[] args) throws Exception {
List<String> sqls=new ArrayList<String>();
sqls.add("insert into tab3 values(4,1,1,1)");
sqls.add("insert into tab3 values(5,1,1,1)");
sqls.add("insert into tab3 values(6,1,1,1)");
sqls.add("insert into tab3 values(7,1,1,1)");
sqls.add("insert into tab3 values(8,1,1,1)");
sqls.add("insert into tab3 values(9,1,1,1)");
testAdd(sqls);
}
public static void testAdd(List<String> sqls)throws Exception{
Class.forName(driver);
Connection conn=null;
CallableStatement ps=null;
try{
conn=DriverManager.getConnection(url, "scott", "ming7435");
conn.setAutoCommit(false);
GetTime.getTime();
ps=conn.prepareCall("{call TEST_PRO("+sqls+")}");
System.out.println("--");
ps.execute();
System.out.println("33");
conn.commit();
ps.clearBatch();
GetTime.getTime();
}catch(Exception e){
conn.rollback();
}finally{
ps.close();
conn.close();
}
}
}
codeFOR IIN1..SQL_LIST.count
FOR I IN 0..SQL_LIST
改成了FOR I IN 1..SQL_LIST
EXECUTE IMMEDIATE SQL_LIST(I);
create table parent(
id number(10),
name varchar2(100),
title varchar2(10)
);
create table child(
id number(10),
parent_id number(10),
child_name varchar2(100),
child_title varchar2(10),
child_content varchar2(200),
child_time timestamp
);
create sequence seq_p_c_id
minvalue 1
maxvalue 9999999999
start with 1
increment by 1
nocache;
drop type t_child_lst_map;
drop type t_child_lst;
drop type t_parent_lst;
create or replace type t_parent as object (
name varchar2(100),
title varchar2(10)
);
/
create or replace type t_child as object (
child_name varchar2(100),
child_title varchar2(10),
child_content varchar2(200)
);
/
create or replace type t_parent_lst as table of t_parent;
/
create or replace type t_child_lst as table of t_child;
/
create or replace type t_child_lst_map as table of t_child_lst;
/
create or replace procedure proc_ins_parent_child(
i_parent_lst in t_parent_lst, --parent列表
i_child_map_lst in t_child_lst_map, --child列表集合,一个map元素对应一个child_lst,其下标与 parent列表的下标相同。
o_ret out number
) as
var_parent t_parent;
var_child_lst t_child_lst;
var_child t_child;
var_parent_id number;
var_child_id number;
begin
for i in 1..i_parent_lst.count loop
--取得parent各列的值
var_parent := i_parent_lst(i);
--取得parent_id;
select seq_p_c_id.nextVal into var_parent_id from dual;
--插入parent表
insert into parent(
id,
name,
title
)
values(
var_parent_id,
var_parent.name,
var_parent.title
);
--取得该parent对应的child列表
var_child_lst := i_child_map_lst(i);
for j in 1..var_child_lst.count loop
var_child := var_child_lst(j);
--取得child_id;
select seq_p_c_id.nextVal into var_child_id from dual;
--插入child表
insert into child(
id,
parent_id,
child_name,
child_title,
child_content,
child_time
)
values(
var_child_id,
var_parent_id,
var_child.child_name,
var_child.child_title,
var_child.child_content,
systimestamp
);
end loop;
end loop;
o_ret := 0;
exception when others then
begin
o_ret := -1;
raise;
end;
end proc_ins_parent_child;
/
ps=conn.prepareCall("{call PRO("+sqls+")}");