Spring JDBC: Returning 0 or 1 rows
java, jdbc, jdbctemplate, spring, spring-jdbc
Solution
There is
DataAccessUtils.singleResult(jdbcTemplate.queryForList(...));
which I believe is made exactly for these situations. It will return `null` if the collection is empty and throw an `IncorrectResultSizeDataAccessException` if more than 1 element found.
Problem
I tried to google this question but could not find: Is there a recommended method in Spring jdbcTemplate which should be used when we expect 0 or 1 rows to be returned. queryForObject() will throw exception when no rows returned. queryForList() will require iterating through list, which is not a problem though. But am curious if there is a preferred/recommended method for 0 or 1 rows returned. thanks!