pageconn.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 208 行
JAVA
208 行
package cn.js.fan.db;import java.util.Vector;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import org.apache.log4j.Logger;import java.util.*;public class PageConn { int rowcount = 0; int colcount = 0; int pageSize = 10; public int curPage = 1; public long total = 0; Logger logger; HashMap mapIndex; String connname; public PageConn(String connname) { logger = Logger.getLogger(PageConn.class.getName()); mapIndex = new HashMap(); this.connname = connname; } public PageConn(String connname, int curPage, int pageSize) { logger = Logger.getLogger(PageConn.class.getName()); mapIndex = new HashMap(); this.curPage = curPage; this.pageSize = pageSize; this.connname = connname; } public long getTotal() { return total; } protected void finalize() throws Throwable { super.finalize(); } public int getColumncount() { return colcount; } public int getRowcount() { return rowcount; } public Vector executeQuery(String sql) { rowcount = 0; colcount = 0; ResultSet rs = null; Vector result = null; Conn conn = new Conn(connname); try { rs = conn.executeQuery(sql); if (rs == null) { return null; } else { ResultSetMetaData rm = rs.getMetaData(); colcount = rm.getColumnCount(); for (int i = 1; i <= colcount; i++) { mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i)); } result = new Vector(); ResultWrapper rsw = new ResultWrapper(rs); while (rsw.next()) { Vector row = new Vector(); for (int i = 0; i < colcount; i++) row.addElement(rsw.getObject(i + 1)); result.addElement(row); rowcount++; } } } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug(e.getMessage()); } return null; } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } return result; } public Vector executeQuery(String sql, int curPage, int pageSize) { this.curPage = curPage; this.pageSize = pageSize; rowcount = 0; colcount = 0; ResultSet rs = null; Vector result = null; Conn conn = new Conn(connname); try { String countsql = SQLFilter.getCountSql(sql); rs = conn.executeQuery(countsql); if (rs != null && rs.next()) { total = rs.getLong(1); } if (rs != null) { rs.close(); rs = null; } int totalpages = (int) Math.ceil((double) total / pageSize); if (curPage > totalpages) curPage = totalpages; if (curPage <= 0) curPage = 1; if (total != 0) conn.setMaxRows(curPage * pageSize); rs = conn.executeQuery(sql); if (rs == null) { return null; } else { ResultSetMetaData rm = rs.getMetaData(); colcount = rm.getColumnCount(); for (int i = 1; i <= colcount; i++) { mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i)); } rs.setFetchSize(pageSize); int absoluteLocation = pageSize * (curPage - 1) + 1; if (rs.absolute(absoluteLocation) == false) { return null; } result = new Vector(); ResultWrapper rsw = new ResultWrapper(rs); do { Vector row = new Vector(); for (int i = 0; i < colcount; i++) row.addElement(rsw.getObject(i + 1)); result.addElement(row); rowcount++; } while (rsw.next()); } } catch (Exception e) { logger.error(e.getMessage()); return null; } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } return result; } public ResultIterator getAllResultIterator(String sql) { Vector r = executeQuery(sql); return new ResultIterator(r, mapIndex); } public ResultIterator getResultIterator(String sql) { Vector r = executeQuery(sql, curPage, pageSize); return new ResultIterator(r, mapIndex, total); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?