首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理iBatis NestedSQLException

问处理iBatis NestedSQLException
EN

Stack Overflow用户
提问于 2012-11-28 18:17:51
回答 2查看 1.9K关注 0票数 3

我有一个java应用程序,它试图向表中插入一行,当我试图为唯一键约束插入重复数据时,com.​ibatis.​common.​jdbc.​exception.NestedSQLException抛出了原因com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException。

如何捕获该异常?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-28 18:22:55

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

代码语言:javascript
复制
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
    }
}
票数 3
EN

Stack Overflow用户

发布于 2013-03-22 22:19:55

以防它能帮上什么人。@tibtof的是正确的,并让我:

代码语言:javascript
复制
public int insert(MyObject myObject) {
    int recCount = -1;
    try {
        recCount = insert(myObject, MyObjectMapper.class);
    } catch (Throwable e) {
        Throwable t = e;
        while (t.getCause() != null) {
            t = t.getCause();
            if (t instanceof SQLIntegrityConstraintViolationException) {
                // get out gracefully.
                recCount = -1;
                return recCount;
            }
        }
        //Something else wicked wrong happened. 
        LogUtils.error(log, e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    }
    return webGroup.getWebGroupId().intValue();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13602701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档