码迷,mamicode.com
首页 > 数据库 > 详细

调用 jdbcTemplate.queryForList 时出现错误 spring-org.springframework.jdbc.IncorrectResultSetColumnCountException

时间:2014-11-17 19:16:37      阅读:530      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   sp   java   for   数据   div   

查找到解决方法: http://www.cnblogs.com/wumian/articles/2012-10-25-2056.html

调用spring jdbc接口:

User user = (User) this.jdbcTemplate.queryForObject(
                "SELECT * FROM USER WHERE ID = 1", User.class);

 报错如下:

Exception in thread "main" org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 4
    at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:87)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:445)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:395)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:463)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:471)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:476)
    at com.ylp.dao.UserDao.getById(UserDao.java:45)
    at com.ylp.dao.Test.main(Test.java:17)

 大概意思是:查出的数据有4列,它把jdbc.queryForObject("SELECT * FROM USER WHERE ID = 1", User.class);中的第一列转换成User.class,所以 expected 1, actual 6,预期1列,但查出来的数据有4列

修改:

RowMapper<User> rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
        User user = (User) this.jdbcTemplate.queryForObject(
                "SELECT * FROM USER WHERE ID = 1", rm);

调用 jdbcTemplate.queryForList 时出现错误 spring-org.springframework.jdbc.IncorrectResultSetColumnCountException

标签:blog   http   io   ar   sp   java   for   数据   div   

原文地址:http://www.cnblogs.com/AloneSword/p/4104146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!