Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JDBC template throws an Incorrect result size exception

I am getting an exception when I run the jdbctemplate to get the id from my table. The exception is:

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

I have the script as below:

CREATE TABLE LOGIN(
PERSON_ID SERIAL PRIMARY KEY, 
USERNAME VARCHAR(20) CHECK (USERNAME IS NOT NULL),
PASSWORD VARCHAR(20) CHECK (PASSWORD IS NOT NULL)
);

and the jdbctemplate code is:

@Override
public int getPersonId(UsernamePassword usernamePassword) {
    return jdbcTemplate.queryForObject("SELECT PERSON_ID FROM LOGIN WHERE USERNAME = ? AND PASSWORD = ?", Integer.class,
            usernamePassword.getUser_name(), usernamePassword.getPassword());

}

I also tried other methods that jdbctemplate provides, but I had no luck. I will appreciate any help. Thanks.

like image 797
Yogesh Ghimire Avatar asked Jan 28 '26 20:01

Yogesh Ghimire


1 Answers

JdbcTemplate's queryForObject expects that executed query will return only one row. If you get 0 rows or more than 1 row that will result in IncorrectResultSizeDataAccessException.

I guess in your case, queryForObject is returning o rows or more than 1 row,

So if you don't want to catch this IncorrectResultSizeDataAccessException, go for query method instead.

like image 115
shankarsh15 Avatar answered Jan 30 '26 12:01

shankarsh15



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!