JdbcTemplate queryForObject query result set quantity, jdbctemplate result set

Source: Internet
Author: User

JdbcTemplate queryForObject query result set quantity, jdbctemplate result set

1. Organize SQL statements, query parameter arrays, and set the return type

public int countByCondtion(String title, int mediaType, String currentStatus, String provider, String region,            Date from, Date to) {        List<Object> params = new ArrayList<Object>();                StringBuffer sql = new StringBuffer();        sql.append("select count(1) from checkin_request where 1=1 ");        if(StringUtils.isNotEmpty(title)){            sql.append("and title=? ");            params.add(title);        }        if(mediaType !=-1){            sql.append("and mediatype=? ");            params.add(mediaType);        }        if(StringUtils.isNotEmpty(currentStatus)){            sql.append("and current_status=? ");            params.add(currentStatus);        }        if(StringUtils.isNotEmpty(provider)){            sql.append("and provider=? ");            params.add(provider);        }        if(StringUtils.isNotEmpty(region)){            sql.append("and region=? ");            params.add(region);        }        if(from !=null){            sql.append("and createtime>=? ");            params.add(from);        }        if(to !=null){            sql.append("and createtime<? ");            params.add(to);        }                        Object[] para = params.toArray(new Object[params.size()]);        return this.jdbcTemplate.queryForObject(sql.toString(), para, Integer.class);            }

2. Problems:

The preceding code is in the correct format. If the select count statement is incorrectly written as select *, an error occurs in the query.

Related Article

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.