org.springframework.jdbc.IncorrectResultSetColumnCountException:列数不正确:预期为1,实际为38

时间:2015-10-08 06:31:25

标签: java spring jdbctemplate

我正在使用JdbcTemplate从数据库中检索Bean。这是我的方法:

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

并且trackerv3Livedata bean结构如下:

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 */
}

在我的场景中,完全可能不会对我的查询产生影响所以我的问题是如何解决以下错误消息。

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

在我看来,我应该回到null而不是抛出异常。我怎样才能解决这个问题?提前谢谢。

4 个答案:

答案 0 :(得分:16)

发生这种情况是因为您使用的queryForList方法不支持多列。请参阅JdbcTemplate

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

getsingleColumnRowMapper()方法创建一个新的RowMapper,用于从单个列读取结果对象。 您可以使用下面给出的方法。

public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException        
 {
    return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
}

答案 1 :(得分:3)

实现Bean RowMapper接口和maprow函数解决了这个问题

public class Mmitrackerv3LivedataMapper implements RowMapper<Mmitrackerv3Livedata> {

@Override
public Mmitrackerv3Livedata mapRow(ResultSet rs, int rowNum)
        throws SQLException {
}

现在我有改变JDBC模板

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

谢谢@abhishek

答案 2 :(得分:2)

应尝试使用Rowmapper,就像使用query API一样。

希望能解决你的问题。

答案 3 :(得分:2)

JdbcTemplate方法

queryForList(String sql, Class<T> elementType)

对于单列查询很有用,您只能指定列类型。如果您在ResultSet中需要多个列,则使用会更准确

query(String sql, RowMapper<T> rowMapper)

作为RowMapper的实现,您可以使用自己的实现,或

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

因此您的情况可能是:

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