rmconn.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 285 行
JAVA
285 行
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.*;import java.sql.PreparedStatement;public class RMConn { int rowCount = 0; int colCount = 0; int pageSize = 10; public int curPage = 1; public long total = 0; Logger logger; HashMap mapIndex; String connname; public Conn pconn = null; public RMConn(String connname) { logger = Logger.getLogger(RMConn.class.getName()); mapIndex = new HashMap(); this.connname = connname; } public RMConn(String connname, int curPage, int pageSize) { logger = Logger.getLogger(RMConn.class.getName()); mapIndex = new HashMap(); this.curPage = curPage; this.pageSize = pageSize; this.connname = connname; } public long getTotal() { return total; } protected void finalize() throws Throwable { if (pconn != null) { pconn.close(); pconn = null; } super.finalize(); } public int getColumnCount() { return colCount; } public int getRowCount() { return rowCount; } public ResultIterator executeQuery(String sql) throws SQLException { rowCount = 0; colCount = 0; ResultSet rs = null; Conn conn = new Conn(connname); Vector result = null; 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 (SQLException e) { throw e; } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } return new ResultIterator(result, mapIndex); } public ResultIterator executePreQuery() throws SQLException { rowCount = 0; colCount = 0; ResultSet rs = null; Vector result = null; try { rs = pconn.executePreQuery(); 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 (SQLException e) { throw e; } finally { if (pconn!=null) { pconn.close(); pconn = null; } } return new ResultIterator(result, mapIndex); } public ResultIterator executeQuery(String sql, int curPage, int pageSize) throws SQLException { 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 (SQLException e) { throw e; } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } return new ResultIterator(result, mapIndex, total); } public int executeUpdate(String sql) throws SQLException { Conn conn = new Conn(connname); int r = 0; try { r = conn.executeUpdate(sql); } catch (SQLException e) { throw e; } finally { if (conn != null) { conn.close(); conn = null; } } return r; } public int executePreUpdate() throws SQLException { int r = 0; try { r = pconn.executePreUpdate(); } catch (SQLException e) { throw e; } finally { if (pconn != null) { pconn.close(); pconn = null; } } return r; } public PreparedStatement prepareStatement(String sql) throws SQLException { if (pconn!=null) { pconn.close(); pconn = null; } try { pconn = new Conn(connname); pconn.pstmt = pconn.prepareStatement(sql); } catch (SQLException e) { if (pconn!=null) { pconn.close(); pconn = null; } throw e; } return pconn.pstmt; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?