Skip to content
Advertisement

Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

I am using Jdbctemplate to retrieve a single String value from the db. Here is my method.

JavaScript

In my scenario it is complete possible to NOT get a hit on my query so my question is how do I get around the following error message.

JavaScript

It would seem to me that I should just get back a null instead of throwing an exception. How can I fix this?

Advertisement

Answer

In JdbcTemplate , queryForInt, queryForLong, queryForObject all such methods expects that executed query will return one and only one row. If you get no rows or more than one row that will result in IncorrectResultSizeDataAccessException . Now the correct way is not to catch this exception or EmptyResultDataAccessException, but make sure the query you are using should return only one row. If at all it is not possible then use query method instead.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement