如何调用我的程序在java中发生编译错误
Posted
技术标签:
【中文标题】如何调用我的程序在java中发生编译错误【英文标题】:how to call my procedure compile error occurred in java 【发布时间】:2013-10-25 10:01:59 【问题描述】:我试图通过 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
【问题讨论】:
简要描述您的问题?把所需的代码和java stacktrace ?如果不知道问题,您的整个程序代码就没有意义? java 错误/代码链接对我不起作用 您的con.prepareCall
有问题。只需输入 package.procedurename,它就会调用所需的过程。您不需要添加BEGIN
和END
。
@Rachcha 我检查并更改了但错误相同。我认为程序错误。我对 pl/sql 代码做了一点评论,请帮助我
【参考方案1】:
这个程序片段是错误的:
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
。
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;
【讨论】:
@kordirko 先生,我按照上面的方法进行了更改,但现在我收到了错误LINE/COL ERROR 32/4 PL/SQL: Statement ignored 32/24 PLS-00302: component 'TSNAME' must be declared PLS-00302: component 'PERCENTAGE' must be declared PL/SQL: Statement ignored 75/30 PLS-00302: component 'LOCKING_ID' must be declared 76/6 PL/SQL: Statement ignored 76/30 PLS-00302: component 'WAITING_ID' must be declared PLS-00302: component 'PROGRAM_WAIT' must be declared
,但我已在代码上方声明它们请帮助我
好的,不需要声明,谢谢你对我的支持和建议,谢谢以上是关于如何调用我的程序在java中发生编译错误的主要内容,如果未能解决你的问题,请参考以下文章
在我调用 r.exec 以启动 cmd 提示符后如何停止执行我的 java 程序
当两个应用程序同时访问一个localspark会话实例时发生错误。调用o2402.defaultParallelism
小白求救 我的系统是win7 64位 在安装visual stdio6.0时发生错误 之后程序编译显示2 error(s), 1 warning(s