IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 38
Asked Answered
A

5

27

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.

Aggy answered 8/10, 2015 at 6:31 Comment(0)
F
49

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));
}
Fenian answered 19/12, 2018 at 10:21 Comment(0)
M
25

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));
}
Marmalade answered 26/4, 2017 at 16:35 Comment(0)
A
4

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

Aggy answered 8/10, 2015 at 8:52 Comment(0)
S
2

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

Hope that solves your problem.

Suppuration answered 8/10, 2015 at 7:58 Comment(0)
A
0

You have formed a select query with * which is a wildcard

Specify the column names in your select query as it's expecting one column, but it's getting 38 columns.

If that don't fix, then try using

    @Autowired
        private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<trackerv3Livedata>  getTrackerData() {
    MapSqlParameterSource parameters = new MapSqlParameterSource("aid", aid);
    
    String sql = "select columnName from mmitrackerv3_livedata where accountid =:aid)";
    
    List<trackerv3Livedata> live = namedParameterJdbcTemplate.query(sql, parameters, (ResultSet resultSet, int rowIndex) -> resultSet.getInt("deviceid"));
    
            
    return live;
}
Andesine answered 12/3 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.