javaBean实现分页显示

867阅读 0评论2011-12-07 认真的鱼123
分类:

  1. import java.sql.*;
  2. import javax.servlet.http.*;
  3. import database.GetConnection;

  4. public class PageQuery {
  5.   
  6.   int offset; // 记录偏移量

  7.   int total; // 记录总数

  8.   
  9.   int maxline; // 记录每页显示记录数

  10.   ResultSet rs; // 读出的结果


  11.   int tpages; // 总页数

  12.   int cpages; // 当前页数

  13.   String pagequery; // 分页显示要传递的参数

  14.   String query; // query 语句

  15.   String querypart; // " from " 以后的 query 部分

  16.   String str= "";
  17.   String str1= "";
  18.   String filepath;
  19.   
  20.   Connection conn; // object of dbclass

  21.   
  22.   //constructer do nothing

  23.   public PageQuery() throws Exception{
  24.     // 每页显示十行

  25.     maxline = 10;
  26.     conn=(Connection)new GetConnection().getConnection();
  27.   }
  28.   
  29.   //********读取记录***************

  30.   // 主要工作函数,根据所给的条件从表中读取相应的记录


  31.   public ResultSet myquery(String category,String name,HttpServletRequest req) throws SQLException {
  32.     
  33.     String query_part, os;
  34.     int begin;
  35.     String query = "select * from " + name + " where category='" + category + "' order by id desc";
  36.     // 截取 " from " 以后的 query 语句

  37.     begin = query.indexOf("from");
  38.     query_part = query.substring(begin, query.length()).trim();
  39.     
  40.     // 计算偏移量

  41.     os = req.getParameter("offset");
  42.     if (os == null) offset = 0;
  43.     else offset = Integer.parseInt(os);
  44.   
  45.     // 获取文件名

  46.     filepath = req.getRequestURI();

  47.     querypart = query_part;
  48.     
  49.     // 计算总的记录条数

  50.     String sql = "select count(*) as total " + this.querypart;
  51.     PreparedStatement psmt=conn.prepareStatement(sql);
  52.    
  53.     rs = psmt.executeQuery(sql);
  54.     if (rs.next())
  55.     total = rs.getInt(1);

  56.     // 设置当前页数和总页数

  57.     tpages = (int)Math.ceil((double)this.total/this.maxline);
  58.     cpages = (int)Math.floor((double)offset/this.maxline+1);

  59.     // 根据条件判断,取出所需记录

  60.     if (total > 0) {
  61.       sql = query + " limit " + offset + " , " + maxline;
  62.       psmt=conn.prepareStatement(sql);
  63.       rs = psmt.executeQuery(sql);
  64.     }
  65.     return rs;
  66.   }

  67.   // 显示总页数

  68.   public int gettotalpages() {
  69.     return tpages;
  70.   }

  71.   //显示当前所在页数

  72.   public int getcurrenpages() {
  73.     return cpages;
  74.   }
  75.   public ResultSet myqueryEM(HttpServletRequest req) throws SQLException {
  76.     
  77.      String query_part, os;
  78.      int begin;
  79.      String query = "select * from employee order by id desc";
  80.      // 截取 " from " 以后的 query 语句

  81.      begin = query.indexOf("from");
  82.      query_part = query.substring(begin, query.length()).trim();
  83.     
  84.      // 计算偏移量

  85.      os = req.getParameter("offset");
  86.      if (os == null) offset = 0;
  87.      else offset = Integer.parseInt(os);
  88.     
  89.      // 获取文件名

  90.      filepath = req.getRequestURI();

  91.      querypart = query_part;
  92.     
  93.      // 计算总的记录条数

  94.      String sql = "select count(*) as total " + this.querypart;
  95.      PreparedStatement psmt=conn.prepareStatement(sql);
  96.     
  97.      rs = psmt.executeQuery(sql);
  98.      if (rs.next())
  99.      total = rs.getInt(1);

  100.      // 设置当前页数和总页数

  101.      tpages = (int)Math.ceil((double)this.total/this.maxline);
  102.      cpages = (int)Math.floor((double)offset/this.maxline+1);

  103.      // 根据条件判断,取出所需记录

  104.      if (total > 0) {
  105.      sql = query + " limit " + offset + " , " + maxline;
  106.      psmt=conn.prepareStatement(sql);
  107.      rs = psmt.executeQuery(sql);
  108.      }
  109.      return rs;
  110.      }

  111.   //**********显示翻页提示栏*************

  112.   // 显示首页、下页、上页、尾页

  113.   // 你可以改成你喜欢的样式

  114.   public String pagelegend() {


  115.      int first, next, prev, last;
  116.      first = 0;
  117.      next = offset + maxline;
  118.     
  119.     
  120.      prev = offset - maxline;
  121.      last = (this.tpages - 1) * maxline;
  122.     
  123.      if(offset >= maxline)
  124.      str += " + filepath + "?offset=" + first + ">首页 ";
  125.      else str += " 首页 ";
  126.      if(prev >= 0)
  127.      str += " + filepath + "?offset=" + prev + ">前页 ";
  128.      else str += " 前页 ";
  129.      if(next < total)
  130.      str += " + filepath + "?offset=" + next + ">后页 ";
  131.      else str += " 后页 ";
  132.      if(tpages != 0 && cpages < tpages)
  133.      str += " + filepath + "?offset=" + last + ">尾页";
  134.      else str += " 尾页 ";

  135.      str += " 页次:" + getcurrenpages() + "/" + gettotalpages() + "页 ";
  136.      str += maxline + "条/页 " + "共" + total + "条";
  137.      return str;
  138.      }
  139. }
上一篇:ubuntu系统数据共享方法
下一篇:SAP界面菜单名称修改