我试图通过java调用我的oracle 11g pl / sql包它会产生错误。请帮帮我。 这是关于pl / sql过程,java程序和java中的错误的所有信息。
程序已成功创建,并且测试了SQL查询。
/*
* create package called "final_package"
* create PROCEDURE called "final_procedure"
* var1 is input parameter from application
* dbinfo is out parameter to application
*/
CREATE OR REPLACE PACKAGE final_package as
PROCEDURE final_procedure(var1 in varchar2, dbinfo out varchar2);
END final_package;
/
CREATE OR REPLACE PACKAGE BODY final_package IS
PROCEDURE final_procedure(var1 in varchar2, dbinfo out varchar2) IS
BEGIN
IF var1 = 'a' --input variable checking--
------ /* get tablespaces name, percentage */ ----
THEN
DECLARE
tsname varchar2(500); ---- /* put tablespaces name to this tsinfo */----
percentage varchar2(500); ---- /* put tablespaces percentage to this tsinfo */----
--declare cursor tbspinfo--
CURSOR tbspinfo is select a.TABLESPACE_NAME as
Tablespace,round((1-((a.BYTES-nvl(b.BYTES,0))/a.BYTES))*100,2)
AS
Percentages from (select TABLESPACE_NAME, sum(BYTES) BYTES from
sys.dba_data_files group by TABLESPACE_NAME) a,
(select
TABLESPACE_NAME, sum(BYTES) BYTES from sys.dba_free_space
group by
TABLESPACE_NAME) b
where
a.TABLESPACE_NAME = b.TABLESPACE_NAME (+)
order by ((a.BYTES-b.BYTES)/a.BYTES) desc;
BEGIN
FOR each_data1 in tbspinfo
LOOP
-- FETCH tsname,percentage INTO tsinfo ---
FETCH tbspinfo INTO tsname, percentage;
-- add tsinfo INTO out parameter "dbinfo" ---
dbinfo := tsname || percentage;
END LOOP;
CLOSE tbspinfo;
END;
---- /* get database lock status */ ----
ELSIF var1 = 'b' THEN
DECLARE
locking_id varchar2(1500);
waiting_id varchar2(1500);
status varchar2(150);
program_hold varchar2(300);
program_wait varchar2(300);
--declare cursor lock_info--
CURSOR lock_info is SELECT vh.sid locking_sid,
vw.sid waiter_sid,
vs.status status,
vs.program program_holding,
vsw.program program_waiting
FROM v$lock vh,
v$lock vw,
v$session vs,
v$session vsw
WHERE(vh.id1, vh.id2) IN (SELECT id1, id2
FROM v$lock
WHERE request = 0
INTERSECT
SELECT id1, id2
FROM v$lock
WHERE lmode = 0)
AND vh.id1 = vw.id1
AND vh.id2 = vw.id2
AND vh.request = 0
AND vw.lmode = 0
AND vh.sid = vs.sid
AND vw.sid = vsw.sid;
BEGIN
FOR each_data in lock_info
LOOP
-- /* put database lock_info to varchar2 variables "locking_id,waiting_id,status,program_hold,program_wait" */ --
FETCH lock_info INTO locking_id,waiting_id,status,program_hold,program_wait;
END LOOP;
CLOSE lock_info;
-- /* add lock_info INTO out parameter "dbinfo" */ ---
dbinfo := locking_id || waiting_id || status || program_hold || program_wait;
END;
END IF;
END;
END;
/
java代码是
CallableStatement clbstmt = con.prepareCall("BEGIN final_package.final_procedure(?,?); END;");
clbstmt.setString(1, var1);
clbstmt.registerOutParameter(2, OracleTypes.VARCHAR);
clbstmt.execute(); // error comes at this
abc = clbstmt.getNString(Integer.SIZE);
abb = clbstmt.getNString(Integer.SIZE);
java错误是
SEVERE: null
java.sql.SQLException: ORA-01001: invalid cursor
ORA-06512: at "SYS.FINAL_PACKAGE", line 71
答案 0 :(得分:0)
这个程序的片段是错误的:
FOR each_data IN lock_info
LOOP
-- put database lock_info to varchar2 variables "locking_id,waiting_id,status,program_hold,program_wait" --
FETCH lock_info INTO locking_id,waiting_id,status,program_hold,program_wait;
END LOOP;
CLOSE lock_info;
使用隐式游标FOR LOOP语句时,请勿使用FETCH
和CLOSE cursor
游标for循环语句打开游标,将行提取到每个迭代(每行)的记录变量中,然后自己关闭游标。您无法关闭光标,因为它已经关闭
有关详细信息,请参阅此链接:http://docs.oracle.com/cd/E11882_01/appdev.112/e17126/static.htm#CHDBJBJE
此代码的另一个问题是它从循环中的游标中获取值,但不处理它们。这没有意义。当代码离开循环时,变量是不确定的 - 它们具有随机值。在循环中获取数据时,代码也必须在此循环中处理获取的数据。
将此代码更改为:
FOR each_data IN lock_info
LOOP
-- FETCH lock_info INTO locking_id,waiting_id,status,program_hold,program_wait;
locking_id := each_data.locking_id;
waiting_id := each_data.waiting_id;
status := each_data.status;
program_hold := each_data.program_hold;
program_wait := each_data.program_wait;
-- Process fetched data here
END LOOP;
-- CLOSE lock_info;
或者你可以使用显式游标循环:
OPEN lock_info;
LOOP
FETCH lock_info INTO locking_id,waiting_id,status,program_hold,program_wait;
EXIT WHEN lock_info%NOTFOUND;
-- Process fetched data here
END LOOP;
CLOSE lock_info;