我的 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:
divisor is equal to zero 消息,因此我无法记录它。但是,如果出现异常,例如未找到表导致过程退出,则 Java 代码会显示它。如何记录 ORA-01476
错误?
最佳答案
实际上你并没有抛出异常,你只是用 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;
然后你会看到在 block 中发生的一些错误 SQLException
关于java - 当过程不停止时,Java SQL 异常不显示 Oracle 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9929071/