What does JdbcTemplate do when RowMapper returns null?
You can simply remove the null from the list after populating with the RowMapper.
rows = JdbcTemplate.query(sql, args, rowMapper);
rows.removeAll(Collections.singletonList(null));
This is the piece of code that adds rows to the result list
public class RowMapperResultSetExtractor<T> implements ResultSetExtractor<List<T>> {
...
public List<T> extractData(ResultSet rs) throws SQLException {
List<T> results = (this.rowsExpected > 0 ? new ArrayList<T>(this.rowsExpected) : new ArrayList<T>());
int rowNum = 0;
while (rs.next()) {
results.add(this.rowMapper.mapRow(rs, rowNum++));
}
return results;
}
...
as we can see it will really add null. However there is no reason why RowMapper should ever return null unless there is a bug in it.
When you return null then it indeed adds this null to the list, also, if you throw an SQLException it is "wrapped" in a Exception that extends RuntimeException, for you not to have to include explicitly a try-catch statement if you don't want to.