简体   繁体   中英

Error while querying for a column in database using spring

I am trying to query an entire column data for eg:

SELECT USER_USERNAME FROM  xxxx WHERE USER_USERNAME=?

I'm getting error

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

My Dao

@Override
public String getAllUsers(UserRegistration uname) {
    System.out.println(uname.getUserName());

    return template.queryForObject(GET_USER_USERNAME, new Object[] { uname.getUserName() },
            new BeanPropertyRowMapper<String>(String.class));

}

I'm injecting the properties through xml file.

my controller

@RequestMapping(method = RequestMethod.POST,value = "/checkUserName", headers = "Accept=application/json")
public org.weber.nag.model.UserRegistration checkUserName(@RequestBody org.weber.nag.model.UserRegistration userReg) {
    userDao.getAllUsers(userReg);
    return userReg;
}

So from the above when i am trying to pass the username from postman it takes the values to controller and from there I'm passing it to my dao to compare whether the name exits or not.The name successfully reaches my dao but I get an error.

So I tried to catch the exception

@Override
public String getAllUsers(UserRegistration uname) {
    System.out.println(uname.getUserName());
    try {
        return template.queryForObject(GET_USER_USERNAME, new Object[] { uname.getUserName() },
                new BeanPropertyRowMapper<String>(String.class));
    } catch (EmptyResultDataAccessException e) {
        System.out.println("uname already exists");
        return "user exists";
    }


}

But every time it prints

"uname already exists"

irrespective of the username given whether it is there in db or not.

In JdbcTemplate , queryForInt, queryForLong, queryForObject all such methods expects that executed query will return one and only one row . If you get no rows that will result in EmptyResultDataAccessException.

From the javadoc of EmptyResultDataAccessException

Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned.

Make sure the query you are using should return only one row .

If at all it is not possible then use query method instead of queryForObject.

Tip: To debug this, run the same query in an SQL IDE directly .

@Override
public String getAllUsers(UserRegistration uname) {
    try {
         template.queryForObject(GET_USER_USERNAME, new Object[] { uname.getUserName() },
                new BeanPropertyRowMapper<String>(String.class));
        System.out.println("uname  exists");
        return "user name is NOT available.";
    } catch (EmptyResultDataAccessException e) {
        System.out.println("uname do not exists");
    }

    return "user is available";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM