java – 处理iBatis NestedSQLException

我有一个java应用程序试图在表中插入一行和com.ibatis.common.jdbc.exception.NestedSQLException抛出原因com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException
当我尝试为唯一键约束插入dublicate数据时.

我如何捕获该异常?

解决方法:

要找到根本原因,你可以这样做:

try {
    //insert
} catch (NestedSQLException e) {
    Throwable t = e;
    while(t.getCause() != null) {
        t = t.getCause();
    }
    //in your situation, now t should be MySQLIntegrityConstraintViolationException 
    if (t instanceOf MySQLIntegrityConstraintViolationException) {
        //do something
    }
}
上一篇:java – 为什么我在插入数据时收到SqlMapException?


下一篇:java – 如何在应用程序级别管理只读数据库连接