`

分页代码

阅读更多
法一:使用hibernate框架分页。

接口如下:

 public List getDAOInfoList(String hql,int start, int pageSize){
  List list=new ArrayList();
  try{
   Query query=this.getSession().createQuery(hql);
   query.setFirstResult(start);
   query.setMaxResults(pageSize );
   list=query.list() ;
  }catch (DataAccessResourceFailureException e) {
   e.printStackTrace();
  } catch (HibernateException e) {
   e.printStackTrace();
  } catch (IllegalStateException e) {
   e.printStackTrace();
  }
  return list;
 }


法二:实现JDBC分页。

接口如下:

public List findListByJdbc(String sql,int start, int pageSize){
 
  List lists = new ArrayList();

  Connection conn=null;
  Statement state=null;
     ResultSet rs = null;
  int lastEnd = start+pageSize;
  
  String sqlPage ="select * from ( "+
      "select x.*,rownum r from ( "+sql+" )x)y where y.r>"+start+" and y.r<="+lastEnd + " order by y.id";
  Session session = this.getSession();
     try {
      conn = session.connection();
   conn.setAutoCommit(false);
   state = conn.createStatement(); 
   rs = state.executeQuery(sqlPage);
   rs.next();
     } catch (Exception e) {
   e.printStackTrace();
  }finally{
   rs.close();
   state.close();
   conn.close();
   session.close();

  }
  return lists;
 }




控制层代码实现(struts1.x)  :

 
/**
   * 分页相关参数
   */
        int pn = Integer.parseInt((String)session.getAttribute("pn"));//配置参数,每页显示pn条
        int pageSize = pn;
        int start = 0; //记录开始数
        int count = 0; //总记录数
        String nowPage = request.getParameter("nowPage");//当前第nowPage页
        if (nowPage != null&&nowPage!=""&&!nowPage.equals("null")) {
         start = (Integer.parseInt(nowPage)-1)*pn;
  }


--笔记谨上 By 2010-04-13---
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics