Using JSP to implement simple SQL reports

Source: Internet
Author: User
Tags final

This content is also prepared according to the requirements of the business students. In fact, this small project is just graduated from the time to do, many times we want to perform the following sql/hql and then get an HTML table output:

Input: Select ID as number, name as name, age as aged from XXX

Output:

Number Name Age
     

The requirement is that if the SQL changes, still show all the other name segments and data.

Because now hibernate used more widely, so the priority is to use Hibernate to achieve, the results found that if the entity mapping query statements, can be easily used: List query.getreturnaliases () to get aliases, However, we know that the query sometimes is very complex, not all hql, at this time with SQLQuery, surprised to hint that this method has not been implemented (the latest version of the Hibernate 3.3 has not been tested), the version is Hibernate 3.2, the corresponding code is:

DAO

/**
 * 根据查询语句返回结果, 并包含结果的列名
 * @param hql
 *  @param args
 * @return
 */
public List queryAllForReport( final String  hql, final Object... args) {
  List list = getHibernateTemplate().executeFind (new HibernateCallback() {
     public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
     Query query =  session.createQuery(hql);

     for(int i =0; i < args.length;  i++) {
       query.setParameter(i, args[i]);
     }


     List list = query.list();
     list.add(0,  query.getReturnAliases());

     return list;
     }
     });

  // Hibernate做count计算返回一般都是对象

  return list;
}

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.