java调用过程不执行

Tomhahaha1 2010-01-08 02:35:27
废话少说,看代码:
数据端,
1.自定义可变数组:
CREATE OR REPLACE TYPE SQLS AS TABLE OF VARCHAR2(2000);


2.过程:

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;
/



3.Java端调用过程:

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();
}
}
}


代码描述:
有一个集合List<String>,每一个元素都是一个insert 语句,想把这个List传给过程循环执行。
问题:运行后,在控制台只打印出"--"就没执行下去了.高手帮忙.
...全文
157 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazylaa 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ming7435 的回复:]
Java code
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
minvalue1
maxvalue9999999999
start with1
increment by1
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;
beginfor i in1..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 in1..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;/
我老早就搜到了你这例子,但是里面有些东西看不懂,麻烦解释一下,上面的crazy..
我明白了分给你,呵呵
[/Quote]

分不是问题。
我看到最近oracle版块对oracle的index by 表,自定义object之类的比较多,我准备blog弄个全面点的文章。
Tomhahaha1 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ming7435 的回复:]
我把SQL codeFOR IIN0..SQL_LIST改成了SQL codeFOR IIN1..SQL_LIST
问题依旧,不报错,数据不插入.哪位高手用我的这个例子帮我做个DEMO,万分感激!
[/Quote]
写错了,应该是
codeFOR IIN1..SQL_LIST.count
Tomhahaha1 2010-01-08
  • 打赏
  • 举报
回复
我把
FOR I IN 0..SQL_LIST
改成了
FOR I IN 1..SQL_LIST

问题依旧,不报错,数据不插入.哪位高手用我的这个例子帮我做个DEMO,万分感激!
huangyunzeng2008 2010-01-08
  • 打赏
  • 举报
回复
首先for写的不对是不用怀疑的,这样估计程序不执行的。
duqiangcise 2010-01-08
  • 打赏
  • 举报
回复
EXECUTE IMMEDIATE SQL_LIST(I);

list表中装的什么哟,能不能用‘EXECUTE IMMEDIATE ’哟。
是不是从list中取出来的内容是一些根本不能够被执行的字符串。
Tomhahaha1 2010-01-08
  • 打赏
  • 举报
回复

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;
/

我老早就搜到了你这例子,但是里面有些东西看不懂,麻烦解释一下,上面的crazy..
我明白了分给你,呵呵
crazylaa 2010-01-08
  • 打赏
  • 举报
回复
卡卡你的存储过程搞错了,数据库下标从1开始,你这里肯定要死翘翘。。。
FOR I IN 0..SQL_LIST.COUNT LOOP
SambaGao 2010-01-08
  • 打赏
  • 举报
回复
以上的异常是你的程序。
SambaGao 2010-01-08
  • 打赏
  • 举报
回复
java.sql.SQLException: ORA-06550: line 1, column 11:
PLS-00103: Encountered the symbol "[" when expecting one of the following:

( ) - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
table avg count current exists max min prior sql stddev sum
variance execute multiset the both leading trailing forall
merge year month DAY_ hour minute second timezone_hour
timezone_minute timezone_region timezone_abbr time timestamp
interval date
<a string literal with character set specification>

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java)
at JDBCTest.testAdd(JDBCTest.java:31)
at JDBCTest.main(JDBCTest.java:18)
crazylaa 2010-01-08
  • 打赏
  • 举报
回复
数组不是这么弄的,搞明白下面的sql及java调用,以后就不愁了:数组得先取得描述。

sql:
http://blog.csdn.net/crazylaa/archive/2009/11/28/4897354.aspx
java:
http://blog.csdn.net/crazylaa/archive/2009/11/28/4897361.aspx
Tomhahaha1 2010-01-08
  • 打赏
  • 举报
回复
不报异常.
SambaGao 2010-01-08
  • 打赏
  • 举报
回复
把异常打出来看看。
wtmiao000 2010-01-08
  • 打赏
  • 举报
回复
http://read.newbooks.com.cn/info/143961.html
这里写的很清楚了还有。
你的TEST_PRO名字和PRO没有对应上
ps=conn.prepareCall("{call TEST_PRO("+sqls+")}");
相当于调用了sqls的toString方法。钟馗后sqls会变成[insert into tab3 values(4,1,1,1), insert into tab3 values(5,1,1,1), insert into tab3 values(6,1,1,1), insert into tab3 values(7,1,1,1), insert into tab3 values(8,1,1,1), insert into tab3 values(9,1,1,1)]
当然是不对的了,
另外你testAdd方法中的catch没有e.printStackTrace();异常也没有打印出来啊!
Tomhahaha1 2010-01-08
  • 打赏
  • 举报
回复
调用的过程名我更正一下
ps=conn.prepareCall("{call PRO("+sqls+")}");

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧