Incorrect result size: expected 1, actual 0 에러메시지 해결
Spring 에서 queryForObject() (queryForMap() 도 결국 queryForObject() 를 호출하기 때문에 동일함)사용했는데, 쿼리 결과값이 1 이어야 하는데 실제 결과값은 0 일때 다음과 같은 에러가 발생한다.
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
이 경우에는 try, catch 를 사용하면 해결 가능하다.
import org.springframework.dao.EmptyResultDataAccessException;
...
String sql = new String("select uId from Users u where userId = ? limit 1");
try {
return (Integer)jdbcTemplate.queryForObject(sql, new Object[] {userId}, Integer.class);
} catch (EmptyResultDataAccessException e) {
return 0;
}
queryForObject() 메소드는 반드시 try, catch 를 사용해야 한다. query() 를 사용할 경우에는 마지막 인자로 ResultSet 이나 RowMapper 를 구현해야하기 때문에 try, catch 가 필요없다.
출처:
http://www.coderanch.com/t/468501/Spring/handling-simpleJdbcTemplate-error