pagebean.java

来自「新闻发布系统」· Java 代码 · 共 132 行

JAVA
132 行
字号
package pageBean;

import java.sql.*;
import java.util.*;
import shen.*;


public class PageBean {
  private int curPage=1 ; //当前是第几页
  private int maxPage; //一共有多少页
  private int maxRowCount; //一共有多少行
  public int rowsPerPage = 5; //每页多少行
  private int parentTitle=10;
  Connection conn = null;
  public java.util.Vector data;

  public PageBean() throws Exception {
    this.setPageBean();
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void setParentTitle(int parentTitle)
  {
      this.parentTitle=parentTitle;
  }

  public int getParentTitle()
  {
      return this.parentTitle;
  }

  public void setMaxPage(int maxPage) {

    this.maxPage = maxPage;
  }

  public int getMaxPage() {
    return this.maxPage;
  }

  public void setCurPage(int curPage) {
    this.curPage = curPage;
  }

  public int getCurPage() {
    return this.curPage;
  }

  public void setMaxRowCount(int maxRowCount) {
    this.maxRowCount = maxRowCount;
  }

  public int getMaxRowCount() {

    return this.maxRowCount;

  }

  public PageBean getResult(String page) throws Exception { //得到要显示于本页的数据
    try {
      PageBean pageBean = new PageBean();
      Vector v = new Vector();
      int pageNum = Integer.parseInt(page);
      Statement stmt = conn.createStatement();
      String strSql = "select top " + pageNum * pageBean.rowsPerPage +
          " * from SecondLevelTitle where parentTitle='"+pageBean.parentTitle+"'";     //where parentTitle='"+pageBean.getParentTitle()+"'"
      ResultSet rset = stmt.executeQuery(strSql);
      int i = 0;
      while (rset.next()) {
        if (i > (pageNum - 1) * pageBean.rowsPerPage - 1) {

          Object[] obj = new Object[4];
          obj[0]=rset.getString(1);
          obj[1] = rset.getString(2);
          obj[2] = rset.getString(4);
          obj[3] = rset.getString(5);
          v.add(obj);
        }
        i++;
      }

      rset.close();
      stmt.close();

      pageBean.setCurPage(pageNum);
      pageBean.data = v;
      return pageBean;
    }
    catch (Exception e) {
      e.printStackTrace();
      throw e;

    }
  }

  public int getAvailableCount() throws Exception {
    int ret = 0;
    PageBean pageBean = new PageBean();
    DbConn cBean =new DbConn();
    conn = cBean.getConn();
    Statement stmt = conn.createStatement();
    String strSql = "select * from SecondLevelTitle where parentTitle='"+pageBean.parentTitle+"'" ;  // where parentTile='"+pageBean.getParentTitle()+"'"
    ResultSet rset = stmt.executeQuery(strSql);
    while (rset.next()) {
      ret++;
    }
    return ret;

  }

  public void setPageBean() throws Exception {

    //得到总行数
    this.setMaxRowCount(this.getAvailableCount());

    if (this.maxRowCount % this.rowsPerPage == 0) { //根据总行数计算总页数
      this.maxPage = this.maxRowCount / this.rowsPerPage;
    }
    else {
      this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
    }
  }

  private void jbInit() throws Exception {
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?