JSP中分页bean

来源:互联网 发布:c语言输出菱形 编辑:程序博客网 时间:2024/05/21 10:26

 

//PageDivBean.java

import java.sql.*;

public class PageDivBean {
  ResultSet rs=null;
  Connection con=null;
  Statement stmt=null;
  String tabName="mdatas";
 
  int pageRecord=10;//指定每页所要显示的记录数
  int requestPage=1; //指定所要查看的页数
  int totalPages=1; //指定要显示全部数据时需要的页数
 
     public String getTabName() {
   return tabName;
  }
  public void setTabName(String tabName) {
   this.tabName = tabName;
  }
  public ResultSet getRs() {
  
   //根据用户请求查看的页数计算该页面的第一条记录
   int requestRecord=(requestPage-1)*pageRecord;
  
   //拼写能够将当前页面所要显示的数据全部选择出来的SQL语句
  
   String sql="select * from "+tabName+" limit "+requestRecord+","+pageRecord;
   try {
   rs=stmt.executeQuery(sql);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return rs;
 }
 public void setRs(ResultSet rs) {
  this.rs = rs;
 }
 public Connection getCon() {
  return con;
 }
 public void setCon(Connection con) {
  this.con = con;
  try {
   //根据获得的链接建立运行SQL语句的容器
   stmt=con.createStatement();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public Statement getStmt() {
  return stmt;
 }
 public void setStmt(Statement stmt) {
  this.stmt = stmt;
 }
 public int getPageRecord() {
  return pageRecord;
 }
 public void setPageRecord(int pageRecord) {
  this.pageRecord = pageRecord;
 }
 public int getRequestPage() {
  return requestPage;
 }
 public void setRequestPage(int requestPage) {
  this.requestPage = requestPage;
 }
 public int getTotalPages() {  //用于计算要显示全部数据时需要的页数的方法
  
  int totalRecords=getTotalRecords();
  
  //通过总记录条数和每页显示的记录条数计算需要多少页可以将所有数据显示出来
     if(totalRecords%pageRecord==0){
        
         totalPages=totalRecords/pageRecord;
     }else{
         totalPages=totalRecords/pageRecord+1;
     }
  
  return totalPages;
 }
 
 //获取总数据条数的方法
 private int getTotalRecords() {
  // TODO Auto-generated method stub
  int totalRecords=0;
  try {
   //用count()函数讲总记录条数计算出来
   ResultSet rs=stmt.executeQuery("select count(*) from "+tabName);
   rs.next();
   
   //获取总记录条数并存储在totalRecords中
   totalRecords=rs.getInt(1);
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return totalRecords;
 }
 public void setTotalPage(int totalPages) {
  this.totalPages = totalPages;
 }


}

原创粉丝点击