IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 38

user4894180 picture user4894180 · Oct 8, 2015 · Viewed 37k times · Source

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

public List<trackerv3Livedata>  getTrackerData() {
    return List<trackerv3Livedata> live = (List<trackerv3Livedata>) jdbcTemplate.queryForList("select * from mmitrackerv3_livedata where accountid =?",new Object[]{aid}, trackerv3Livedata.class);
}

And trackerv3Livedata bean Structure Are Following:

public class trackerv3Livedata implements Serializable {

    private static final long serialVersionUID = 2409168269491619888L;

    private int deviceid;
    private Long timestamp;
    private Mmitrackerv3Device mmitrackerv3Device;
    private Mmitrackerv3Account mmitrackerv3Account;
    private double latitude;
    private double longitude;
    private Double altitude;
    private Double speedkph;
    private Double heading;
    private Double gpssignal;
    private Integer geozoneid;
    private Double distancekm;
    private Double gsmsignal;
    private Double mainpower;
    private Integer laststatustime;
    private Double internalbattry;
    private Double temperature;
    private Short dinput1;
    private Short dinput2;
    private Short dinput3;
    private Short dinput4;
    private Short dinput5;
    private Short dinput6;
    private Short dinput7;
    private Short dinput8;
    private Short ainput1;
    private Short ainput2;
    private Short ainput3;
    private Short ainput4;
    private Short doutput1;
    private Short doutput2;
    private Short doutput3;
    private Short doutput4;

    /* There are Some Getter And Setter Method With Constructor */
}

In my scenario it is complete possible to NOT get a hit on my query so my question is how do I get around the following error message.

org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 38

It would seem to me that I should just get back a null instead of throwing an exception. How can I fix this? Thanks in advance.

Answer

ajith george picture ajith george · Apr 26, 2017

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));
}