java 如何解决 SpringBatch 中的 org.springframework.dao.EmptyResultDataAccessException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17788539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:59:50  来源:igfitidea点击:

How to resolve org.springframework.dao.EmptyResultDataAccessException in SpringBatch

java

提问by user2335416

springframework.dao.EmptyResultDataAccessExceptionwhile trying to select the data from the sql server database, here is the code I have written. Can anybody suggest how to select data from database using query interface?

springframework.dao.EmptyResultDataAccessException在尝试从 sql server 数据库中选择数据时,这是我编写的代码。有人可以建议如何使用查询界面从数据库中选择数据吗?

public int getRedempRequestId(RedemptionResponseBean redemptionResponse)
        throws ParseException {
    final Timestamp redempIdFromCsv = getRedeemDate(redemptionResponse);
    int participantId = redemptionResponse.getOcpEventMemberId();
    System.out.println(redempIdFromCsv);
    System.out.println(participantId);

    int res = jdbcTemplate.queryForInt("SELECT RR.REDEMPTION_REQUEST_ID "
            + "FROM  dbo.REDEMPTION_REQUEST RR WITH (NOLOCK)"
            + "WHERE RR.SENT_DATE = ? AND "
            + "RR.OEDEF_OCP_EVENT_MEMBER_ID = ? ", new Object[] {
            redempIdFromCsv, participantId });
    System.out.println(res+"-----------");
    return res;
}

could anybody tell me what's the problem here?

有人能告诉我这里有什么问题吗?

回答by mthmulders

If you expect no data to be returned, you shouldn't use jdbcTemplate.queryForInt(...), since that method expects the query it will execute willreturn an integer value. Basically, that is what the EmptyResultDataAccessExceptionis telling you; its Javadoc says:

如果您不希望返回任何数据,则不应使用jdbcTemplate.queryForInt(...),因为该方法期望它将执行的查询返回一个整数值。基本上,这EmptyResultDataAccessException就是告诉你的;它的 Javadoc 说:

Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned.

当预期结果至少有一行(或元素)但实际返回零行(或元素)时抛出数据访问异常。

Instead, you should use one of the query(...)methods from JdbcTemplate, which allow for zero rows to be returned.

相反,您应该使用其中一种query(...)方法 from JdbcTemplate,它允许返回零行。