我的java代码调用一个过程,如果有任何异常,它就会在java SQLEXCEPTION中被捕获。如果存在导致过程停止的异常,则一切正常,但是如果异常没有停止,则过程java不会显示我们想要记录的错误。下面是一个示例:
操作步骤:
create or replace procedure test_jdbc(Table_name IN VARCHAR2) is
v_sql VARCHAR2(50);
cursor c_test is
select employee_id, employee_num from employee where rownum < 11;
v_test c_test%rowtype;
BEGIN
for v_test in c_test loop
begin
dbms_output.put_line(v_test.employee_id || ' - ' ||
v_test.employee_num);
dbms_output.put_line('c_test%rowcount - ' || c_test%rowcount);
if c_test%rowcount = 8 then
v_sql := v_test.employee_id / 0;
end if;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end test_jdbc;这在运行时会产生以下输出:
0 - 1
c_test%rowcount - 1
0 - 2
c_test%rowcount - 2
0 - 3
c_test%rowcount - 3
0 - 4
c_test%rowcount - 4
0 - 5
c_test%rowcount - 5
0 - 6
c_test%rowcount - 6
0 - 7
c_test%rowcount - 7
0 - 8
c_test%rowcount - 8
ORA-01476: divisor is equal to zero
0 - 9
c_test%rowcount - 9
0 - 10
c_test%rowcount - 10
PL/SQL procedure successfully completed下面是调用该过程的java代码:
String insertStoreProc = "{call test_jdbc(?)}";
try {
dbConnection = getDBConnection();
callablestatement = dbConnection.prepareCall(insertStoreProc);
callablestatement.setString(1, "Employee");
// execute select SQL stetement
callablestatement.execute();
System.out.println("Procedure Complete!");
} catch (SQLException e) {
e.printStackTrace(System.err);
System.err.println("SQLState: " +
((SQLException)e).getSQLState());
System.err.println("Error Code: " +
((SQLException)e).getErrorCode());
System.err.println("Message: " + e.getMessage());
}但是,我的java没有显示ORA-01476:除数等于零的消息,因此我无法记录它。但是,如果有一个异常,比如table not found,它会导致过程退出,那么java代码就会显示它。如何记录ORA-01476错误?
发布于 2012-03-30 00:08:54
实际上你并没有抛出异常,你只是用dbms_output包输出它们。
begin
-- my stuff
when others then
dbms_output.put_line(sqlerrm); -- here is just a output, procedure will continue
end;尝试以下代码(使用raise):
begin
-- my stuff
when others then
dbms_output.put_line(sqlerrm);
raise;
end;然后,您将看到在使用SQLException的块中发生的一些错误
发布于 2012-03-30 00:09:43
您正在处理Oracle存储过程中的异常。
这就是为什么不传播到客户端的原因。
我的建议是删除Oracle过程中的异常块或添加RAISE_APPLICATION_ERROR()
当其他人发生异常时,
dbms_output.put_line(sqlerrm);
RAISE_APPLICATION_ERROR(-21000,"Oops division by zero ")
end;https://stackoverflow.com/questions/9929071
复制相似问题