Search code examples
mysqlsqlspring-dataspring-jdbcjdbctemplate

how to retrieve a list using like statement in JDBCTemplate ?


I try to return a list of data with an entered word, but it returns null

    public List<people> peoples(String l) {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    try {
        String sql = "select * from people where peopleName like '?' ;";
        List<people> peoples = jdbcTemplate.query(sql, new Object[]{"%"+l+"%"},
                new BeanPropertyRowMapper<>(people.class));
        return peoples;

    } catch (DataAccessException e) {

    }catch(NullPointerException nullPointer) {

    }
    return null;
}

Solution

  • Use

    select * from people where peopleName like ?

    instead of

    select * from people where peopleName like '?'

    Don't use the " ' " value before and after placeholder value ? . It always searches the peoplename like ? not with your value