我的java代碼調用一個過程,並且如果有任何異常,它會被java SQLEXCEPTION捕獲。一切正常,如果有一個異常,導致過程停止,但如果異常不會停止過程java不會顯示我們要記錄的錯誤。這裏有一個例子:Oracle異常不被Java SQL顯示異常當程序不停止
步驟:
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:
除數等於零消息,因此我不能記錄它。但是,如果沒有找到像say table這樣的異常,導致過程退出java代碼,則會顯示它。我怎樣才能記錄ORA-01476
錯誤?
那麼實際上提出的是要幫我得到的異常在Java中,但我客戶端想知道是否有方法可以捕獲錯誤消息,如果它碰到異常塊並且不會引發異常並且允許該過程繼續。現在他們做的方式是通過將輸出參數傳遞給存在'dbms_output.put_line(sqlerrm)'的java來記錄它。 – Eosphorus 2012-03-29 16:38:32
所以我想這是做到這一點的唯一方法。如果我們不引發異常,並且我們希望java知道它通過out參數傳遞錯誤或引發異常並讓SQLEXception捕獲它。有沒有其他方式不涉及參數? – Eosphorus 2012-03-29 16:38:54
在表中插入錯誤是另一種選擇。 – 2012-03-29 17:00:43