Spring JDBC 를 사용하게 될 경우 만나는 Exception 중 하나인 IncorrectResultSizeDataAccessException 는
queryForObject의 결과가 없을 때(null) 혹은 2개 이상일 때 발생한다.
아래 예시 코드를 보면
try { JdbcTemplate jdbcTemplate; String sql = "select name from UserTable where name = ?"; return jdbcTemplate.queryForObject(sql, String.class, "thenice"); } catch (IncorrectResultSizeDataAccessException error) { return null; } |
thenice에 대한 값이 1개일 경우엔 문제가 없지만 없거나 2개 이상일 경우에는 JDBC는 어떤값을 가져와야할지 모르기에 이런 에러가 발생한다.
이런식으로 에러가 발생할 수 있다.
Incorrect result size: expected 1, actual 0
그럼 위 코드처럼 try catch 로 묶을때 IncorrectResultSizeDataAccessException 를 같이 묶어주면 된다.
IncorrectResultSizeDataAccessException (Spring Framework 6.1.6 API)
Constructor for IncorrectResultSizeDataAccessException. Constructor for IncorrectResultSizeDataAccessException. Constructor for IncorrectResultSizeDataAccessException. Constructor for IncorrectResultSizeDataAccessException. Constructor for IncorrectResultS
docs.spring.io
참고 포스팅
https://thenicesj.tistory.com/550
try catch와 throws, throw의 차이
이전 포스팅에서 try catch 에 대해 다룬 포스팅이 있다. 자세한 내용은 아래 참고 포스팅을 참고하면 된다. 이번 포스팅에서는 예외 처리를 위해서 좀더 try catch throws throw 에 대해 알아볼 것이다.
thenicesj.tistory.com
https://thenicesj.tistory.com/485
Error, Checked Exception, Unchecked Exception 비교
포스팅 시작 앞에 에러와 예외에 대해서 먼저 비교를 해보고 시작하려고 한다. Error란 시스템 레벨에서 발생하는 심각한 수준의 오류를 뜻하며 개발자가 미리 예측하여 처리할 수 없기에 개발
thenicesj.tistory.com
'IT > Java' 카테고리의 다른 글
Workbook 사용법 (20) | 2024.04.28 |
---|---|
openfeign에서 header에 값 추가 (21) | 2024.04.24 |
@RequestPart? (22) | 2024.04.22 |
File To MultipartFile / MultipartFile To File (29) | 2024.04.21 |
멀티파트(Multipart)란? (19) | 2024.04.20 |
댓글