⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showbypage.java

📁 计算机技术的快速发展
💻 JAVA
字号:
package com.suninformation.tools;

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

import com.suninformation.user.*;

public class ShowByPage {
  private boolean showDelete; //是否显示删除标记
  private boolean showTitleText; //是否显示标题文字
  private boolean mouseMove; //鼠标移动到行时是否有动作
  private String mouseMoveRowColor; //鼠标移动到行时行的背景色
  private String tableBgColor; //表格背景色
  private String tableStyle; //表格样式
  private String rowStyle; //行(表格体)样式
  private String colStyle; //列(单元格)样式
  private String tableHeadBgColor; //表头背景色
  private int pageSize; //每页显示记录数
  private String cols; //字段集合
  private String colNames; //显示字段名集合
  private int rows; //总记录数
  private String nowPage; //当前页数
  private int pages; //总页数
  private String rowAfterClickToURL; //行单击后执行跳转到哪个页面
  private int colCount; //字段总数
  private String scriptPathName; //当前执行脚本的文件路径及文件名(相对路径)
  private String titleTextStyle; //标题文字样式
  private String tableName; //表名
  private String where; //条件
  private String orderBy; //排序方法
  private boolean showTableHead; //是否显示表头
  private String colsShow; //哪个字段时否显示
  private StringTokenizer icols, icolsShow, icolNames;
  private Connection conn;

  private String tableTitleString = ""; //标题文字临时字符串
  private String tableString = "";

  public ShowByPage(Connection conn) {
    this.conn = conn;
  }

  public String createBar() throws UnacceptableException {
    String barstr="no";
    if(scriptPathName==null || scriptPathName=="") {
      throw new UnacceptableException("scriptPathName属性无效。");
    } else {
      String btn_1 = "首页";
      if(Integer.parseInt(nowPage)>1) {
        btn_1 = "<a href='" + this.scriptPathName + "?page=1" + "'>首页</a>";
      }
      String btn_2 = "下一页";
      if(Integer.parseInt(nowPage)<pages) {
        btn_2 = "<a href='" + this.scriptPathName + "?page=" + (Integer.parseInt(nowPage)+1) + "'>下一页</a>";
      }
      String btn_3 = "上一页";
      if(Integer.parseInt(nowPage)>1) {
        btn_3 = "<a href='" + this.scriptPathName + "?page=" + (Integer.parseInt(nowPage)-1) + "'>上一页</a>";
      }
      String btn_4 = "尾页";
      if(Integer.parseInt(nowPage)!=pages) {
        btn_4 = "<a href='" + this.scriptPathName + "?page=" + pages + "'>尾页</a>";
      }
      barstr="<table width=100%><tr><td align=right>" + btn_1 +" | " + btn_3 + " | " + btn_2 +" | " + btn_4 + " </td></tr></table>";
    }
    return barstr;
  }

  /**
   * 生成SQL语句
   *
   * @return String
   */
  private String createSQL() {
    String sqltemp = "SELECT " + cols + " FROM " + tableName;
    if (where != null && where != "") {
      sqltemp += " WHERE " + where;
    }
    if (orderBy != null && orderBy != "") {
      sqltemp += " ORDER BY " + orderBy;
    }
    return sqltemp;
  }

  public String show() throws UnacceptableException {

    //处理字段名及显示字段名
    this.icols = new StringTokenizer(cols, ",");
    this.icolNames = new StringTokenizer(colNames, ",");
    this.icolsShow = new StringTokenizer(colsShow, ",");
    this.colCount = this.icols.countTokens();

    Statement stmt = null;
    ResultSet rs = null;
    try {
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                  ResultSet.CONCUR_READ_ONLY);
      //返回可滚动的结果集
      rs = stmt.executeQuery(createSQL());
      if(!rs.next()) {
        throw new UnacceptableException("没有任何记录符合条件。");
      }
      //将游标移动到最后一行
      rs.last();
      //获取最后一行的行号
      int lastRow = rs.getRow();
      //每页显示的记录数
      //int pageSize = 5;
      //计算分页后的总页数
      int pageCount = (lastRow % pageSize == 0) ? (lastRow / pageSize) :
          (lastRow / pageSize + 1);
      //当前显示的初始页数
      int showPage = 1;
      //获取用户想要显示的页数
      String integer = nowPage;
      if (integer == null) {
        integer = "1";
      }
      try {
        showPage = Integer.parseInt(integer);
      }
      catch (NumberFormatException e) {
        showPage = 1;
      }
      if (showPage <= 1) {
        showPage = 1;
      }
      if (showPage >= pageCount) {
        showPage = pageCount;
      }
      //设定当前页数属性、总页数属性、总记录数属性
      this.nowPage = String.valueOf(showPage);
      this.pages = pageCount;
      this.rows = lastRow;

      //如果要显示第showPage页,那么游标应移到posion的值
      int posion = (showPage - 1) * pageSize + 1;
      //设置游标的位置
      rs.absolute(posion);
      //如果显示标题文字,则:所以titleTextStyle的内容中必须含有下面四个带有#..#的字符串
      if (showTitleText) {
        if (titleTextStyle != null && titleTextStyle != "") {
          tableTitleString = titleTextStyle;
          tableTitleString = tableTitleString.replaceAll("#PAGECOUNT#",
              String.valueOf(pageCount));
          tableTitleString = tableTitleString.replaceAll("#PAGESIZE#",
              String.valueOf(pageSize));
          tableTitleString = tableTitleString.replaceAll("#ROWS#",
              String.valueOf(lastRow));
          tableTitleString = tableTitleString.replaceAll("#NOWPAGE#",
              String.valueOf(showPage));
        }
        else {
          throw new UnacceptableException("titleTextStyle属性值无效。");
        }
      }
      //根据tableStyle输出表格头
      if (tableStyle != null && tableStyle != "") {
        if (tableStyle != null && tableStyle != "") {
          tableString = tableStyle;
        }
        else {
          throw new UnacceptableException("titleStyle属性值无效。");
        }
        String onMouseMove = null;
        if (mouseMove) {
          if (mouseMoveRowColor != null && mouseMoveRowColor != "" &&
              tableBgColor != null && tableBgColor != "") {
            onMouseMove = "onmouseover=\"this.style.backgroundColor='" +
                mouseMoveRowColor +
                "'\" style=\"CURSOR: hand\" onmouseout=\"this.style.backgroundColor='" +
                tableBgColor + "'\" align=middle";
          }
          else {
            throw new UnacceptableException(
                "mouseMoveRowColor或tableBgColor属性值无效。");
          }
        }
        if ( (rowStyle == null || rowStyle == "") ||
            (colStyle == null || colStyle == "")) {
          throw new UnacceptableException("rowStyle或colStyle属性值无效。");
        }
        String tableTemp = "";
        String trTemp = rowStyle;
        String tdTemp = colStyle;
        String tmprow = "";
        //输出表格<td>主体
        for (int i = 1; i <= pageSize && !rs.isAfterLast(); i++) {
          String temp = "";
          if (showDelete) {
            temp = "<INPUT type=checkbox value=" + rs.getObject(1).toString() +
                " name=e_id" + i + ">";
            temp = tdTemp.replaceAll("#BODY#", temp);
            temp = temp.replaceAll("#MOUSECLICK#", " ");
          }
          this.icols = new StringTokenizer(cols, ",");
          this.icolsShow = new StringTokenizer(colsShow, ",");
          while (this.icols.hasMoreTokens()) {
            String temp1 = this.icols.nextToken();
            String temp2 = this.icolsShow.nextToken();
            if (temp2.equals("1")) {
              temp += tdTemp.replaceAll("#BODY#", rs.getObject(temp1).toString());
            }
          }
          tmprow += trTemp.replaceAll("#BODY#", temp);
          if (mouseMove) { //添加鼠标移动事件
            tmprow = tmprow.replaceAll("#MOUSEACTIVE#", onMouseMove);
          }
          else {
            tmprow = tmprow.replaceAll("#MOUSEACTIVE#", " ");
          }
          if (rowAfterClickToURL != null && rowAfterClickToURL != "") { //添加鼠标单击事件
            tmprow = tmprow.replaceAll("#MOUSECLICK#",
                                       "onClick=\"javascript:window.open('" +
                                       rowAfterClickToURL + "?args=" +
                                       rs.getObject(1).toString() +
                                       "','open')\"");
          }
          else {
            tmprow = tmprow.replaceAll("#MOUSECLICK#", " ");
          }
          rs.next();
        }
        if (showTableHead) {
          tableTemp = "<tr bgColor=" + tableHeadBgColor + " align=middle>";
          if (showDelete) {
            tableTemp += "<td> </td>";
          }
        }
        this.icolsShow = new StringTokenizer(colsShow, ",");
        while (this.icolNames.hasMoreTokens() && this.icolsShow.hasMoreTokens()) {
          String temp1 = this.icolsShow.nextToken();
          if (temp1.equals("1")) {
            tableTemp += "<td>" + this.icolNames.nextToken() + "</td>";
          }
          else {
            this.icolNames.nextToken();
          }
        }
        if (showTableHead) {
          tableTemp += "</tr>";
        }
        tableString = tableString.replaceAll("#BODY#", tmprow);
        if (showTableHead) {
          tableString = tableString.replaceAll("#HEAD#", tableTemp);
        }
        else {
          tableString = tableString.replaceAll("#HEAD#", " ");
        }
        if (showTitleText) { //如果显示标题文字,则将其加到tableString前面
          tableString = tableTitleString + tableString;
        }
      }
      else {
        throw new UnacceptableException("tableStyle属性值无效。");
      }

    }
    catch (SQLException sqle) {
      throw new UnacceptableException("数据库操作失败。", sqle);
    }
    finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (stmt != null) {
          stmt.close();
        }
      }
      catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return tableString;

  }

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
  public void setShowDelete(boolean showDelete) {
    this.showDelete = showDelete;
  }

  public void setShowTitleText(boolean showTitleText) {
    this.showTitleText = showTitleText;
  }

  public void setMouseMove(boolean mouseMove) {
    this.mouseMove = mouseMove;
  }

  public void setMouseMoveRowColor(String mouseMoveRowColor) {
    this.mouseMoveRowColor = mouseMoveRowColor;
  }

  public void setTableBgColor(String tableBgColor) {

    this.tableBgColor = tableBgColor;
  }

  public void setTableStyle(String tableStyle) {
    this.tableStyle = tableStyle;
  }

  public void setRowStyle(String rowStyle) {
    this.rowStyle = rowStyle;
  }

  public void setColStyle(String colStyle) {
    this.colStyle = colStyle;
  }

  public void setTableHeadBgColor(String tableHeadBgColor) {
    this.tableHeadBgColor = tableHeadBgColor;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public void setCols(String cols) {
    this.cols = cols;
  }

  public void setColNames(String colNames) {
    this.colNames = colNames;
  }

  public void setNowPage(String nowPage) {

    this.nowPage = nowPage;
  }

  public void setPages(int pages) {
    this.pages = pages;
  }

  public void setRowAfterClickToURL(String rowAfterClickToURL) {
    this.rowAfterClickToURL = rowAfterClickToURL;
  }

  public void setScriptPathName(String scriptPathName) {
    this.scriptPathName = scriptPathName;
  }

  public void setTitleTextStyle(String titleTextStyle) {

    this.titleTextStyle = titleTextStyle;
  }

  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  public void setWhere(String where) {
    this.where = where;
  }

  public void setOrderBy(String orderBy) {
    this.orderBy = orderBy;
  }

  public void setShowTableHead(boolean showTableHead) {
    this.showTableHead = showTableHead;
  }

  public void setColsShow(String colsShow) {
    this.colsShow = colsShow;
  }

  public int getPageSize() {
    return pageSize;
  }

  public String getNowPage() {

    return nowPage;
  }

  public int getPages() {
    return pages;
  }

  public int getColCount() {
    return colCount;
  }

  public int getRows() {
    return rows;
  }
}

⌨️ 快捷键说明

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