- package com.dz.tools;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- /**
- * 用於WEB的分頁類(基於ORACLE). 注意:當字段中有","號時,請用"#"號代替 例如:_columnList =
- * "id,to_char('2007-08-05','yyyy-mm-dd')"中
- * to_char('2007-08-05','yyyy-mm-dd')中的","號改爲"#",應寫爲: _columnList =
- * "id,to_char('2007-08-05'#'yyyy-mm-dd')" <br>
- * <br>
- *
- * @author dz
- * @version 版本號 1.00
- */
- public class Page {
- private ArrayList list = new ArrayList();// 返回數據集.
- private int tCount = 0;// 總記錄數.
- private int tPages = 0;// 總頁數.
- private int cPage = 0;// 當前頁.
- private int cMinPage = 0;// 當前最小頁,用於<<.
- private int cMaxPage = 0;// 當前最大頁,用於>>.
- private int offset = 0;// 本頁最小記錄號.
- private int below = 0;// 本頁最大記錄號.
- private String first = "";// 第一頁.
- private String prev = "";// 上一頁.
- private String next = "";// 下一頁.
- private String last = "";// 最後一頁.
- private int specify = 0;// 指定顯示頁.
- private ArrayList pageList = new ArrayList();// 頁列表
- private int pageListCount = 0;// 所列頁總頁
- private int maxLine = 0;// 每頁最大顯示數.
- private String dataSourceName = "";// 數據源名.
- private String tableName = "";// 表名.
- private String columnList = "";// 字段列表.
- private String findCondition = "";// 查詢條件.
- private String pageQuery = "";// 頁面參數.
- /**
- * 該類的構造函數,無參數.
- *
- */
- public Page() {
- }
- /**
- * 設置數據源名.
- *
- * @param dataSourceName
- * 數據庫名
- */
- public void setDataSourceName(String dataSourceName) {
- this.dataSourceName = dataSourceName;
- }
- /**
- * 設置表名.
- *
- * @param tableName
- * 表名
- */
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
- /**
- * 設置字段列表.
- *
- * @param columnList
- * 字段列表
- */
- public void setColumnList(String columnList) {
- this.columnList = columnList;
- }
- /**
- * 設置字段列表.
- *
- * @param columnList[]
- * 字段列表
- */
- public void setColumnList(String[] columnList) {
- for (int i = 0; i < columnList.length; i++)
- if (i == columnList.length - 1)
- this.columnList += columnList[i];
- else
- this.columnList += columnList[i] + ",";
- }
- /**
- * 設置查詢條件
- *
- * @param findCondition
- * 查詢條件
- */
- public void setFindCondition(String findCondition) {
- this.findCondition = findCondition;
- }
- /**
- * 設置頁面傳遞參數,如有多個參數要傳遞,可多次調用.
- *
- * @param key
- * 參數名
- * @param value
- * 參數值
- */
- public void setPageQuery(String key, String value) {
- this.pageQuery += key + "=" + value + "&";
- }
- /**
- * 取頁面參數.
- *
- * @return 頁面參數
- */
- public String getPageQuery() {
- return pageQuery;
- }
- /**
- * 設置每頁最大顯示數
- *
- * @param maxLine
- * 每頁最大顯示數
- */
- public void setMaxLine(int maxLine) {
- this.maxLine = maxLine;
- }
- /**
- * 取總頁數.
- *
- * @return 總頁數
- */
- public int getTPages() {
- return tPages;
- }
- /**
- * 取當前頁.
- *
- * @return 當前頁
- */
- public int getCPage() {
- return cPage;
- }
- public String getcPage() {
- return cPage + "";
- }
- /**
- * 取第一頁.
- *
- * @return 第一頁
- */
- public String getFirst() {
- return first;
- }
- /**
- * 取上一頁.
- *
- * @return 上一頁
- */
- public String getPrev() {
- return prev;
- }
- /**
- * 取下一頁.
- *
- * @return 下一頁
- */
- public String getNext() {
- return next;
- }
- /**
- * 取最後一頁.
- *
- * @return 最後一頁
- */
- public String getLast() {
- return last;
- }
- /**
- * 取總記錄數.
- *
- * @return 總記錄數
- */
- public int getTCount() {
- return tCount;
- }
- public void setSpecify(int specify) {
- this.specify = specify;
- }
- public int getSpecify() {
- return specify;
- }
- /**
- * 生成頁列表. changeCatalog()之後調用
- *
- * @param current
- * @param csum
- * @return
- */
- private void setPageList() {
- int start = 1;
- if (cPage > pageListCount)
- if (cPage % pageListCount == 0)
- start = pageListCount
- * ((int) Math.ceil(cPage / pageListCount) - 1) + 1;
- else
- start = pageListCount * (int) Math.ceil(cPage / pageListCount)
- + 1;
- cMinPage = start - 1;
- int end = start + pageListCount;
- cMaxPage = end;
- if (end > tPages) {
- end = tPages;
- cMaxPage = 0;
- }
- for (int i = 0; i < pageListCount; i++) {
- if (start <= tPages) {
- pageList.add(new Integer(start));
- start++;
- }
- }
- }
- public ArrayList getPageList() {
- return pageList;
- }
- public void setPageListCount(int pageListCount) {
- this.pageListCount = pageListCount;
- }
- public int getCMaxPage() {
- return cMaxPage;
- }
- public int getCMinPage() {
- return cMinPage;
- }
- /**
- * 取記錄集.
- *
- * @return 記錄集
- */
- public ArrayList getList() {
- return list;
- }
- /**
- * 生成本頁參數. 在query()中獲取總記錄數後,執行SQL之前調用.
- */
- private void changeCatalog() {
- // 總頁數
- tPages = (int) Math.ceil((double) tCount / maxLine);
- // 當前頁
- if (specify == 0) {
- specify = 1;
- }
- if (specify > tPages) {
- specify = tPages;
- }
- // 本頁最小記錄號
- offset = (specify - 1) * maxLine + 1;
- // 本頁最大記錄號
- below = specify * maxLine;
- // 當前頁
- cPage = specify;
- // 首頁參數
- if (specify > 1)
- first = "?page=1&" + pageQuery;
- else
- first = "";
- // 上頁參數
- if (specify > 1)
- prev = "?page=" + (specify - 1) + "&" + pageQuery;
- else
- prev = "";
- // 下頁參數
- if (specify < tPages)
- next = "?page=" + (specify + 1) + "&" + pageQuery;
- else
- next = "";
- // 未頁參數
- if (specify < tPages)
- last = "?page=" + tPages + "&" + pageQuery;
- else
- last = "";
- }
- /**
- * 主操作類(使用page). 請在setXXX()之後,getXXX()方法之前調用
- *
- * @throws SQLException
- */
- public void query() throws SQLException {
- // 取數據庫連接
- Connection conn = Database.getConnection(dataSourceName);
- if (conn == null)
- throw new SQLException("獲取數據庫連接錯誤");
- Statement stmt = null;
- ResultSet rs = null;
- try {
- String sql = "SELECT count(*) FROM " + tableName + " "
- + findCondition;
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next())
- tCount = rs.getInt(1);// 總記錄數
- changeCatalog();
- //System.out.println(tCount);
- setPageList();
- // 設置查詢語句
- sql = "SELECT " + columnList.replaceAll("#", ",") + " FROM "
- + tableName + " " + findCondition;
- sql = "SELECT rownum r1,t.* FROM (" + sql + ") t";
- sql = "SELECT r.* FROM (" + sql + ") r WHERE r1 BETWEEN " + offset
- + " AND " + below;
- // System.out.println(specify);
- //System.out.println(sql);
- // 取記錄
- String[] _row = columnList.split(",");
- int _rownum = _row.length;
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- String[] _temp = new String[_rownum];
- for (int i = 0; i < _rownum; i++) {
- _temp[i] = rs.getString(i + 2);
- // System.out.println(_temp[i]);
- }
- list.add(_temp);
- }
- } finally {
- if (rs != null)
- rs.close();
- if (stmt != null)
- stmt.close();
- if (conn != null)
- conn.close();
- }
- }
- }
分頁處理類
java 代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.