Incorrect column count:expected 1, actual 5,jdbctemplate queryforlist error

Source: Internet
Author: User
Tags postgresql redis



Spring JdbcTemplate queryforlist Error


Incorrect column count:expected 1, actual 5

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>





? Copyright Sweet Potato Yiu July 10, 2017

http://www.cnblogs.com/fanshuyao/


First, the problem description:


An error occurred when querying with queryForList in JdbcTemplate, as follows:






The Query method is as follows:


Java code

Jdbctemplate.queryforlist (Selectsql.tostring (), Entityclass)





Query SQL as follows:


SQL code

SELECT * FROM test where 1=1 order by create_time desc limit 0,10





The error is as follows:


Java code

Incorrect column count:expected 1, actual 5








Second, the solution:


1, the reason for the above error is that the query returns a result column expected to be 1, but the actual return is 5 columns, because there are 5 fields in the test table, it returns 5 columns. The explanation for this method parameter is this:


Java code

Parameters:

SQL SQL query to execute

ElementType the required type of element in the result list (for example, Integer. Class)


The 2nd argument on the net is that it can only be a simple type string or integer.






2. Use Query queries


Java code

Jdbctemplate.query (Selectsql.tostring (), RowMapper)


But with one more parameter RowMapper, this parameter needs to be defined as:


Java code
  1. @SuppressWarnings ("unused")
  2. private Beanpropertyrowmapper<t> RowMapper = new Beanpropertyrowmapper<t> (entityclass) {
  3. @Override
  4. protected void Initbeanwrapper (Beanwrapper bw) {
  5. super.initbeanwrapper (BW);
  6. }
  7. };


The specific function is to enter the query results into the entity.






To this point will solve the problem.





>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>



? Copyright Sweet Potato Yiu July 10, 2017



http://www.cnblogs.com/fanshuyao/



Incorrect column count:expected 1, actual 5,jdbctemplate queryforlist error


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.