IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 38

前端 未结 4 1881
刺人心
刺人心 2021-02-12 22:18

I am using JdbcTemplate to retrieve a Bean from the db. Here is my method:

public List  getTrackerData() {
    return List         


        
相关标签:
4条回答
  • 2021-02-12 22:56

    Should try using Rowmapper like use it as when you query API.

    Hope that solves your problem.

    0 讨论(0)
  • 2021-02-12 22:57

    JdbcTemplate method

    queryForList(String sql, Class<T> elementType)
    

    is useful for one-column-queries, you may only specify the column type. If you need more than one column in ResultSet, it's more accurate to use

    query(String sql, RowMapper<T> rowMapper)
    

    as implementation for RowMapper you may use your own, or

    jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(clazz));
    

    so in your case it may be:

    public List<Trackerv3Livedata> getTrackerData() {
        String sql = "SELECT * FROM mmitrackerv3_livedata mlive " +
                     "JOIN mmitrackerv3_device mdevice ON mlive.accountid = " +
                     "mdevice.accountid WHERE mlive.accountid = " + aid;
    
        return jdbcTemplate.query(sql,
            new BeanPropertyRowMapper<Trackerv3Livedata>(Trackerv3Livedata.class));
    }
    
    0 讨论(0)
  • 2021-02-12 22:59

    Implementing Bean RowMapper Interface and maprow function solve this problem

    public class Mmitrackerv3LivedataMapper implements RowMapper<Mmitrackerv3Livedata> {
    
    @Override
    public Mmitrackerv3Livedata mapRow(ResultSet rs, int rowNum)
            throws SQLException {
    }
    

    And Now I have Change In JDBC Template

     List<Mmitrackerv3Livedata> live = jdbcTemplate.query("select * from mmitrackerv3_livedata mlive " + 
     "join mmitrackerv3_device mdevice on mlive.accountid = mdevice.accountid where mlive.accountid = " +
         aid, new Mmitrackerv3LivedataMapper());
    

    Thanks @abhishek

    0 讨论(0)
  • 2021-02-12 23:15

    This is happening because, the queryForList method you have used will not support for multiple columns. See the implementation of queryForList from JdbcTemplate

    public <T> List<T> More ...queryForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException 
    {
        return query(sql, args, getSingleColumnRowMapper(elementType));
    }
    

    The getsingleColumnRowMapper() method creates a new RowMapper for reading result objects from a single column. You can use the method given below instead.

    public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException        
     {
        return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
    }
    
    0 讨论(0)
提交回复
热议问题