java 代码
- 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();
- }
- }
- }