分頁處理類

java 代碼
  1. package com.dz.tools;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.ResultSet;   
  5. import java.sql.SQLException;   
  6. import java.sql.Statement;   
  7. import java.util.ArrayList;   
  8.   
  9. /**  
  10.  * 用於WEB的分頁類(基於ORACLE). 注意:當字段中有","號時,請用"#"號代替 例如:_columnList =  
  11.  * "id,to_char('2007-08-05','yyyy-mm-dd')"中  
  12.  * to_char('2007-08-05','yyyy-mm-dd')中的","號改爲"#",應寫爲: _columnList =  
  13.  * "id,to_char('2007-08-05'#'yyyy-mm-dd')" <br>  
  14.  * <br>  
  15.  *   
  16.  * @author dz  
  17.  * @version 版本號 1.00  
  18.  */  
  19. public class Page {   
  20.   
  21.     private ArrayList list = new ArrayList();// 返回數據集.   
  22.   
  23.     private int tCount = 0;// 總記錄數.   
  24.   
  25.     private int tPages = 0;// 總頁數.   
  26.   
  27.     private int cPage = 0;// 當前頁.   
  28.   
  29.     private int cMinPage = 0;// 當前最小頁,用於<<.   
  30.   
  31.     private int cMaxPage = 0;// 當前最大頁,用於>>.   
  32.   
  33.     private int offset = 0;// 本頁最小記錄號.   
  34.   
  35.     private int below = 0;// 本頁最大記錄號.   
  36.   
  37.     private String first = "";// 第一頁.   
  38.   
  39.     private String prev = "";// 上一頁.   
  40.   
  41.     private String next = "";// 下一頁.   
  42.   
  43.     private String last = "";// 最後一頁.   
  44.   
  45.     private int specify = 0;// 指定顯示頁.   
  46.   
  47.     private ArrayList pageList = new ArrayList();// 頁列表   
  48.   
  49.     private int pageListCount = 0;// 所列頁總頁   
  50.   
  51.     private int maxLine = 0;// 每頁最大顯示數.   
  52.   
  53.     private String dataSourceName = "";// 數據源名.   
  54.   
  55.     private String tableName = "";// 表名.   
  56.   
  57.     private String columnList = "";// 字段列表.   
  58.   
  59.     private String findCondition = "";// 查詢條件.   
  60.   
  61.     private String pageQuery = "";// 頁面參數.   
  62.   
  63.     /**  
  64.      * 該類的構造函數,無參數.  
  65.      *   
  66.      */  
  67.     public Page() {   
  68.     }   
  69.   
  70.     /**  
  71.      * 設置數據源名.  
  72.      *   
  73.      * @param dataSourceName  
  74.      *            數據庫名  
  75.      */  
  76.     public void setDataSourceName(String dataSourceName) {   
  77.         this.dataSourceName = dataSourceName;   
  78.     }   
  79.   
  80.     /**  
  81.      * 設置表名.  
  82.      *   
  83.      * @param tableName  
  84.      *            表名  
  85.      */  
  86.     public void setTableName(String tableName) {   
  87.         this.tableName = tableName;   
  88.     }   
  89.   
  90.     /**  
  91.      * 設置字段列表.  
  92.      *   
  93.      * @param columnList  
  94.      *            字段列表  
  95.      */  
  96.     public void setColumnList(String columnList) {   
  97.         this.columnList = columnList;   
  98.     }   
  99.   
  100.     /**  
  101.      * 設置字段列表.  
  102.      *   
  103.      * @param columnList[]  
  104.      *            字段列表  
  105.      */  
  106.     public void setColumnList(String[] columnList) {   
  107.         for (int i = 0; i < columnList.length; i++)   
  108.             if (i == columnList.length - 1)   
  109.                 this.columnList += columnList[i];   
  110.             else  
  111.                 this.columnList += columnList[i] + ",";   
  112.     }   
  113.   
  114.     /**  
  115.      * 設置查詢條件  
  116.      *   
  117.      * @param findCondition  
  118.      *            查詢條件  
  119.      */  
  120.     public void setFindCondition(String findCondition) {   
  121.         this.findCondition = findCondition;   
  122.     }   
  123.   
  124.     /**  
  125.      * 設置頁面傳遞參數,如有多個參數要傳遞,可多次調用.  
  126.      *   
  127.      * @param key  
  128.      *            參數名  
  129.      * @param value  
  130.      *            參數值  
  131.      */  
  132.     public void setPageQuery(String key, String value) {   
  133.         this.pageQuery += key + "=" + value + "&";   
  134.     }   
  135.   
  136.     /**  
  137.      * 取頁面參數.  
  138.      *   
  139.      * @return 頁面參數  
  140.      */  
  141.     public String getPageQuery() {   
  142.         return pageQuery;   
  143.     }   
  144.   
  145.     /**  
  146.      * 設置每頁最大顯示數  
  147.      *   
  148.      * @param maxLine  
  149.      *            每頁最大顯示數  
  150.      */  
  151.     public void setMaxLine(int maxLine) {   
  152.         this.maxLine = maxLine;   
  153.     }   
  154.   
  155.     /**  
  156.      * 取總頁數.  
  157.      *   
  158.      * @return 總頁數  
  159.      */  
  160.     public int getTPages() {   
  161.         return tPages;   
  162.     }   
  163.   
  164.     /**  
  165.      * 取當前頁.  
  166.      *   
  167.      * @return 當前頁  
  168.      */  
  169.     public int getCPage() {   
  170.         return cPage;   
  171.     }   
  172.   
  173.     public String getcPage() {   
  174.         return cPage + "";   
  175.     }   
  176.   
  177.     /**  
  178.      * 取第一頁.  
  179.      *   
  180.      * @return 第一頁  
  181.      */  
  182.     public String getFirst() {   
  183.         return first;   
  184.     }   
  185.   
  186.     /**  
  187.      * 取上一頁.  
  188.      *   
  189.      * @return 上一頁  
  190.      */  
  191.     public String getPrev() {   
  192.         return prev;   
  193.     }   
  194.   
  195.     /**  
  196.      * 取下一頁.  
  197.      *   
  198.      * @return 下一頁  
  199.      */  
  200.     public String getNext() {   
  201.         return next;   
  202.     }   
  203.   
  204.     /**  
  205.      * 取最後一頁.  
  206.      *   
  207.      * @return 最後一頁  
  208.      */  
  209.     public String getLast() {   
  210.         return last;   
  211.     }   
  212.   
  213.     /**  
  214.      * 取總記錄數.  
  215.      *   
  216.      * @return 總記錄數  
  217.      */  
  218.     public int getTCount() {   
  219.         return tCount;   
  220.     }   
  221.   
  222.     public void setSpecify(int specify) {   
  223.         this.specify = specify;   
  224.     }   
  225.   
  226.     public int getSpecify() {   
  227.         return specify;   
  228.     }   
  229.   
  230.     /**  
  231.      * 生成頁列表. changeCatalog()之後調用  
  232.      *   
  233.      * @param current  
  234.      * @param csum  
  235.      * @return  
  236.      */  
  237.     private void setPageList() {   
  238.   
  239.         int start = 1;   
  240.   
  241.         if (cPage > pageListCount)   
  242.             if (cPage % pageListCount == 0)   
  243.                 start = pageListCount   
  244.                         * ((int) Math.ceil(cPage / pageListCount) - 1) + 1;   
  245.             else  
  246.                 start = pageListCount * (int) Math.ceil(cPage / pageListCount)   
  247.                         + 1;   
  248.   
  249.         cMinPage = start - 1;   
  250.         int end = start + pageListCount;   
  251.         cMaxPage = end;   
  252.         if (end > tPages) {   
  253.             end = tPages;   
  254.             cMaxPage = 0;   
  255.         }   
  256.   
  257.         for (int i = 0; i < pageListCount; i++) {   
  258.             if (start <= tPages) {   
  259.                 pageList.add(new Integer(start));   
  260.                 start++;   
  261.             }   
  262.         }   
  263.   
  264.     }   
  265.   
  266.     public ArrayList getPageList() {   
  267.         return pageList;   
  268.     }   
  269.   
  270.     public void setPageListCount(int pageListCount) {   
  271.         this.pageListCount = pageListCount;   
  272.     }   
  273.   
  274.     public int getCMaxPage() {   
  275.         return cMaxPage;   
  276.     }   
  277.   
  278.     public int getCMinPage() {   
  279.         return cMinPage;   
  280.     }   
  281.   
  282.     /**  
  283.      * 取記錄集.  
  284.      *   
  285.      * @return 記錄集  
  286.      */  
  287.     public ArrayList getList() {   
  288.         return list;   
  289.     }   
  290.   
  291.     /**  
  292.      * 生成本頁參數. 在query()中獲取總記錄數後,執行SQL之前調用.  
  293.      */  
  294.     private void changeCatalog() {   
  295.         // 總頁數   
  296.         tPages = (int) Math.ceil((double) tCount / maxLine);   
  297.   
  298.         // 當前頁   
  299.         if (specify == 0) {   
  300.             specify = 1;   
  301.         }   
  302.         if (specify > tPages) {   
  303.             specify = tPages;   
  304.         }   
  305.   
  306.         // 本頁最小記錄號   
  307.         offset = (specify - 1) * maxLine + 1;   
  308.   
  309.         // 本頁最大記錄號   
  310.         below = specify * maxLine;   
  311.   
  312.         // 當前頁   
  313.         cPage = specify;   
  314.   
  315.         // 首頁參數   
  316.         if (specify > 1)   
  317.             first = "?page=1&" + pageQuery;   
  318.         else  
  319.             first = "";   
  320.         // 上頁參數   
  321.         if (specify > 1)   
  322.             prev = "?page=" + (specify - 1) + "&" + pageQuery;   
  323.         else  
  324.             prev = "";   
  325.         // 下頁參數   
  326.         if (specify < tPages)   
  327.             next = "?page=" + (specify + 1) + "&" + pageQuery;   
  328.         else  
  329.             next = "";   
  330.         // 未頁參數   
  331.         if (specify < tPages)   
  332.             last = "?page=" + tPages + "&" + pageQuery;   
  333.         else  
  334.             last = "";   
  335.     }   
  336.   
  337.     /**  
  338.      * 主操作類(使用page). 請在setXXX()之後,getXXX()方法之前調用  
  339.      *   
  340.      * @throws SQLException  
  341.      */  
  342.     public void query() throws SQLException {   
  343.         // 取數據庫連接   
  344.         Connection conn = Database.getConnection(dataSourceName);   
  345.         if (conn == null)   
  346.             throw new SQLException("獲取數據庫連接錯誤");   
  347.         Statement stmt = null;   
  348.         ResultSet rs = null;   
  349.         try {   
  350.             String sql = "SELECT count(*) FROM " + tableName + " "  
  351.                     + findCondition;   
  352.             stmt = conn.createStatement();   
  353.             rs = stmt.executeQuery(sql);   
  354.             while (rs.next())   
  355.                 tCount = rs.getInt(1);// 總記錄數   
  356.             changeCatalog();   
  357.             //System.out.println(tCount);   
  358.             setPageList();   
  359.             // 設置查詢語句   
  360.             sql = "SELECT " + columnList.replaceAll("#"",") + " FROM "  
  361.                     + tableName + " " + findCondition;   
  362.             sql = "SELECT rownum r1,t.* FROM (" + sql + ") t";   
  363.             sql = "SELECT r.* FROM (" + sql + ") r WHERE r1 BETWEEN " + offset   
  364.                     + " AND " + below;   
  365.             // System.out.println(specify);   
  366.             //System.out.println(sql);   
  367.   
  368.             // 取記錄   
  369.             String[] _row = columnList.split(",");   
  370.             int _rownum = _row.length;   
  371.             rs = stmt.executeQuery(sql);   
  372.             while (rs.next()) {   
  373.                 String[] _temp = new String[_rownum];   
  374.                 for (int i = 0; i < _rownum; i++) {   
  375.                     _temp[i] = rs.getString(i + 2);   
  376.                     // System.out.println(_temp[i]);   
  377.                 }   
  378.                 list.add(_temp);   
  379.             }   
  380.         } finally {   
  381.             if (rs != null)   
  382.                 rs.close();   
  383.             if (stmt != null)   
  384.                 stmt.close();   
  385.             if (conn != null)   
  386.                 conn.close();   
  387.         }   
  388.     }   
  389.   
  390. }   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章