'jdbtemplet.qoeryForObject(String sql,RowMapper action)

String sql = "select * from customers where id = "+id;

Customer c =    jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(Customers.class));

if the id exist it is returning an object perfect. But when the id does not exist I expect an object with null properties but it is throwing exception. Incorrect result size: expected 1, actual 0.



Solution 1:[1]

Use an sql aggregate function like:

String sql = "select max(*) from customers where id = "+id;

As id is supposed to be unique this will return either the value or null if no row is found.

Of cause, the other valid possibility is to catch and handle the exception.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Markus