当前位置:学学看123知识中心电脑教学数据库教程用javabean来实现MySQL的分页显示» 正文

用javabean来实现MySQL的分页显示

[06-11 18:17:26]   来源:http://www.xxk123.com  数据库教程   阅读:8438

导读: //constructer do nothing public PageQuery() { // 每页显示十行 MaxLine = 10; db = new dbClass(); } //读取记录*** //主要工作函数,根据所给的条件从表中读取相应的记录 public ResultSet myQuery(String query, HttpServletRequest req) throws SQLException { String query_part, os; int begin, offset; // 截取 " FROM " 以后的 query 语句 begin = query.indexOf(" FROM "); query_part = query.substring(begin, query.length()).trim(); // 计算偏移量 os = req.g

用javabean来实现MySQL的分页显示,标签:sql数据库教程,access数据库教程,http://www.xxk123.com
  //constructer do nothing
  public PageQuery() {
    // 每页显示十行
    MaxLine = 10;    
    db = new dbClass();    
  }  
  
  //********读取记录***************
  // 主要工作函数,根据所给的条件从表中读取相应的记录   

  public ResultSet myQuery(String query, HttpServletRequest req) throws SQLException {
    
    String query_part, os;
    int begin, offset;
    
    // 截取 " FROM " 以后的 query 语句
    begin = query.indexOf(" FROM ");
    query_part = query.substring(begin, query.length()).trim();    
        
    // 计算偏移量
    os = req.getParameter("offset");
    if (os == null) Offset = 0;
    else Offset = Integer.parseInt(os);
    
    // 获取文件名
    FilePath = req.getRequestURI();
    
    Query = query;
    QueryPart = query_part;    
    
    // 计算总的记录条数
    String SQL = "SELECT Count(*) AS total " + this.QueryPart;
    rs = db.executeQuery(SQL);    
    if (rs.next())
    Total = rs.getInt(1);     

    // 设置当前页数和总页数
    TPages = (int)Math.ceil((double)this.Total/this.MaxLine);
    CPages = (int)Math.floor((double)Offset/this.MaxLine+1);

    // 根据条件判断,取出所需记录
    if (Total > 0) {
      SQL = Query + " LIMIT " + Offset + " , " + MaxLine;
      rs = db.executeQuery(SQL);       
    }    
    return rs;
  }  

  // 显示总页数
  public int getTotalPages() {    
    return TPages;
  }

  //显示当前所在页数
  public int getCurrenPages() {          
    return CPages;
  }

  //**********显示翻页提示栏*************  
  // 显示首页、下页、上页、尾页
  // 你可以改成你喜欢的样式
  public String PageLegend() {    

    String str = "";    
    int first, next, prev, last;
    first = 0;
    next = Offset + MaxLine;
    prev = Offset - MaxLine;

上一页  [1] [2] [3] [4] [5]  下一页


Tag:数据库教程sql数据库教程,access数据库教程电脑教学 - 数据库教程

Copyright 学学看123 All Right Reserved.

1 2 3 4 5 6 7 8 9 10