[Solved] org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected1, actual0

Using queryforobject to query database

Integer id = jdbcTemplate.queryForObject(sql, Integer.class);

The following exception was encountered

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

The causes were found out

Queryforobject has and can only query one piece of data. If there is no such data in the database or the same data in the database, this exception will be thrown

Solution:

Use queryforlist to judge

List<Integer> list = jdbcTemplate.queryForList(sql, Integer.class);

Similar Posts: