首页 文章

验证spring JDBC批量更新是否成功

提问于
浏览
6

我使用Spring JDBCTemplate batchUpdate批量插入数据 . 我想验证数据是否已成功插入 . JDBCTemplate batchUpdate返回一个int [] [],这是验证数据是否插入的正确方法?

link说"All batch update methods return an int array containing the number of affected rows for each batch entry. This count is reported by the JDBC driver and it's not always available in which case the JDBC driver simply returns a -2 value" . 我无法理解在这里返回-2值的重要性 . 这是否意味着插入不成功?

1 回答

  • 11

    -2并不一定意味着错误,可能如上所述,受影响行的计数的情况不可用 .

    EDIT

    -2是Statement.SUCCESS_NO_INFO的值(而EXECUTE_FAILED是-3) . 因此,除非驱动程序不符合JDBC规范,否则-2明确表示成功

    END OF EDIT

    错误通过BatchUpdateException报告

    通常,如果您在批处理脚本中运行N个查询,您将获得结果中每个查询i的更新计数:

    int result[] = jdbcTemplate.batchUpdate(sql);
    

    所以:

    result[0]
    

    将保留第一个查询的更新计数,

    result[1]
    

    将保留第二个查询等的更新计数 .

相关问题