2013-09-25 29 views
0

我是Ibatis的新手。我試圖捕捉SQL異常和錯誤代碼,但我無法找到它。 Ibatis異常是運行時異常。那麼如何找到SQL錯誤消息和錯誤代碼。我捕捉到的異常模樣:如何使用來自Ibatis的消息和錯誤代碼獲取SQLException

com.common.dao.BaseDaoImpl:167 - 錯誤代碼捕獲創建類別 站長:無法插入 - ID [createMaster],parameterObject [com.mslkp.vo.msklpVO @ a27b9e。原因: com.ibatis.common.jdbc.exception.NestedSQLException:

---錯誤發生在com/sql/createMSLKP.xml中。

---應用參數映射時發生錯誤。

---檢查createMSLKP-InlineParameterMap。

---檢查語句(更新失敗)。

---原因:java.sql.SQLIntegrityConstraintViolationException:ORA-01400:(。 「MS」 「T_MSTR_LKUP」 「DELET_FLG」)不能將NULL插入

如何趕上錯誤代碼01400一樣?

+2

趕上'NestedSQLException'(或任何其它包裝例外是),並獲得其'cause'。 –

+0

Actualy com.ibatis.common.jdbc.exception.RuntimeSQLException以及它的子類有一個方法getErrorCode(),試試看 – Jk1

回答

0

抓住iBatis的異常,並通過使鏈條挖,你有錯誤消息告訴您哪裏找:

} catch (RuntimeSQLException e) { 
    if (e.getCause() == null) { 
     throw e; // not what you're interested in, throw it back 
    } 
    Exception nested = e.getCause().getCause(); 
    if (nested instanceof SQLIntegrityConstraintViolationException) { 
     SQLIntegrityConstraintViolationException constraintViolation = (SQLIntegrityConstraintViolationException)nested; 
     String message = constraintViolation.getMessage(); 
     String sqlState = constraintViolation.getSQLState(); 
     int errorCode = constraintViolation.getErrorCode(); 
     // create validation message or whatever 
    } else { 
     throw e; // not what you're interested in, throw it back 
    } 
} 
相關問題