简体   繁体   中英

Jdbc returns empty list but SQL query succesfully gets data [Spring]

I am trying to execute this query:

@Override
public UserInfo get(Long id) {
    String sql = "SELECT * FROM users WHERE id = ? ";
    List<UserInfo> list = jdbcTemplate.query(sql,new UserInfoMapper(),id);
    return list.get(0);
}

but jdbc return empty list and I get exception at return line. But if try to execute directly though the console it returns: Query, Answer

Query was executed with id 1 and retured correct anwser; But in method its returned this

I couldn't find any same questions so that may be point at my inattention to something. But I can't see any problem that may cause this. Thanks in advance;

Updated 1 Changing code to

@Override
public UserInfo get(Long id) {
    String sql = "SELECT * FROM users WHERE id = ? ";
    List<UserInfo> list = jdbcTemplate.query(sql, new Object[] {id},new UserInfoMapper());
    return list.get(0);
}

resulted in same: result Updated 2

@Override
public UserInfo mapRow(ResultSet resultSet, int i) throws SQLException {
    UserInfo info = new UserInfo();
    info.setId(resultSet.getLong("id"));
    info.setFirstname(resultSet.getString("firstname"));
    info.setMiddlename(resultSet.getString("middlename"));
    info.setLastname(resultSet.getString("lastname"));
    info.setUsername(resultSet.getString("username"));
    info.setPassword(resultSet.getString("password"));
    info.setEmail(resultSet.getString("email"));
    info.setMobilephone(resultSet.getString("mobilephone"));
    info.setPosition(resultSet.getString("position"));
    return info;
}
public class UserInfo {
    private Long id;
    private String firstname;
    private String middlename;
    private String lastname;
    private String username;
    private String password;
    private String email;
    private String mobilephone;
    private String position;

    public UserInfo() {
    }
}

Getter and setters for each field is there but I think there is no need to show them up.

Check user credentials that you are using to connect database from your application and the user credentials in console. And also check owner schema , table owner schema in your application.

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