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

简介: 国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。

国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为中国PE第一股,市值超1000亿元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

【推荐看看】 http://my.oschina.net/u/218421/blog/38598

调用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);


提示:以上结论在 spring 4.x 上行不通,主要是 queryForObject 模板方法针对的结果集是“一列”的时候,并且支持的是常见类型(Integer.class)等

 再次查找资料,在官方的 api 中,仅仅提到:

elementType - the required type of element in the result list (for example, Integer.class)

哪如果我想要返回一个 List<T> 的列表对象不行吗?查找资料,发现如下文章将的比较好: 

【推荐看看】 http://my.oschina.net/u/218421/blog/38598

改用如下代码实现即可:

return (new DBUtility()).getJdbcDao().query(sql, new RowMapper<Task>() {
            public Task mapRow(ResultSet arg0, int arg1) throws SQLException {
                Task t = new Task();
                t.setTaskId(arg0.getLong("taskId"));
                t.setTaskName(arg0.getString("taskName"));
                t.setTaskDescription(arg0.getString("taskDescription"));
                t.setTaskPriority(arg0.getString("taskPriority"));
                t.setTaskStatus(arg0.getString("taskStatus"));
                return t;
            }
        });

 

OK,此时才算解决问题。

spring jdbc 版本: 4.1.1.RELEASE

目录
相关文章
|
Java
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
1659 0
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
|
4月前
|
Java Spring
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException(Spring循环依赖问题)
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException(Spring循环依赖问题)
|
6月前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
Java 微服务 Spring
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
208 0
|
druid Java
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
809 0
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
|
6月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
|
6月前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
|
6月前
|
Shell
sh(Spring+Spring mvc+hibernate)——BaseDao.java
sh(Spring+Spring mvc+hibernate)——BaseDao.java
|
6月前
|
Java 编译器 Spring
Spring中@Autowired和@Resource的区别
Spring中@Autowired和@Resource的区别
140 0
|
Java 关系型数据库 MySQL
Spring JDBC-Spring对DAO的支持
Spring JDBC-Spring对DAO的支持
69 0