Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

springframework.dao.EmptyResultDataAccessException while 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?

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?

like image 335
user2335416 Avatar asked Jul 22 '13 13:07

user2335416


1 Answers

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

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.

like image 121
mthmulders Avatar answered Nov 08 '22 05:11

mthmulders