jdbctemplate.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 531 行 · 第 1/2 页
JAVA
531 行
package com.cloudwebsoft.framework.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;import java.sql.Timestamp;import java.sql.Clob;import java.sql.Blob;import cn.js.fan.db.ResultIterator;import cn.js.fan.db.ResultWrapper;import cn.js.fan.db.SQLFilter;import java.sql.Types;import cn.js.fan.web.Global;import com.cloudwebsoft.framework.util.LogUtil;import cn.js.fan.util.StrUtil;public class JdbcTemplate { int rowCount = 0; int colCount = 0; int pageSize = 10; public int curPage = 1; public long total = 0; Logger logger; HashMap mapIndex; Connection connection = null; String connName; public JdbcTemplate() { this.connection = new Connection(Global.defaultDB); connName = connection.connName; logger = Logger.getLogger(JdbcTemplate.class.getName()); mapIndex = new HashMap(); } public JdbcTemplate(Connection conn) { this.connection = conn; connName = connection.connName; logger = Logger.getLogger(JdbcTemplate.class.getName()); mapIndex = new HashMap(); } public JdbcTemplate(DataSource ds) { this.connection = ds.getConnection(); connName = connection.connName; logger = Logger.getLogger(JdbcTemplate.class.getName()); mapIndex = new HashMap(); } public JdbcTemplate(DataSource ds, int curPage, int pageSize) { this.connection = ds.getConnection(); connName = connection.connName; logger = Logger.getLogger(JdbcTemplate.class.getName()); mapIndex = new HashMap(); this.curPage = curPage; this.pageSize = pageSize; } public Connection getConnection() { return this.connection; } public long getTotal() { return total; } public int getColumnCount() { return colCount; } public int getRowCount() { return rowCount; } public ResultIterator executeQuery(String sql) throws SQLException { ResultIterator ri = new ResultIterator(); rowCount = 0; colCount = 0; ResultSet rs = null; Vector result = null; try { checkConnection(); rs = connection.executeQuery(sql); if (rs == null) { return ri; } 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++; } } } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (connection.getAutoCommit()) { connection.close(); connection = null; } } return new ResultIterator(result, mapIndex); } public void addBatch(String sql) throws SQLException { connection.addBatch(sql); } public int[] executeBatch() throws SQLException { int[] r = null; try { checkConnection(); r = connection.executeBatch(); } finally { if (connection.getAutoCommit()) { connection.close(); connection = null; } } return r; } public void fillPreparedStatement(PreparedStatement ps, Object[] objectParams) throws SQLException { if (objectParams == null) return; int len = objectParams.length; for (int i = 1; i <= len; i++) { Object obj = objectParams[i - 1]; if (obj == null) { ps.setNull(i, Types.VARCHAR); } else if (obj instanceof String) { ps.setString(i, (String) obj); } else if (obj instanceof Integer) { ps.setInt(i, ((Integer) obj).intValue()); } else if (obj instanceof java.util.Date) { ps.setTimestamp(i, new Timestamp(((java.util.Date)obj).getTime())); } else if (obj instanceof Timestamp) { ps.setTimestamp(i, (Timestamp) obj); } else if (obj instanceof Long) { ps.setLong(i, ((Long) obj).longValue()); } else if (obj instanceof Short) { ps.setShort(i, ((Short) obj).shortValue()); } else if (obj instanceof Double) { ps.setDouble(i, ((Double) obj).doubleValue()); } else if (obj instanceof Float) { ps.setFloat(i, ((Float) obj).floatValue()); } else if (obj instanceof Clob) { ps.setClob(i, (Clob) obj); } else if (obj instanceof Blob) { ps.setBlob(i, (Blob) obj); } else if (obj instanceof Boolean) { ps.setBoolean(i, ((Boolean) obj).booleanValue()); } else if (obj instanceof Byte) { ps.setByte(i, ((Byte) obj).byteValue()); } else throw new SQLException("fillPreparedStatement: Object " + obj + " type is not supported."); } } public ResultIterator executeQuery(String sql, Object[] objectParams, int curPage, int pageSize) throws SQLException { ResultIterator ri = new ResultIterator(); this.curPage = curPage; this.pageSize = pageSize; rowCount = 0; colCount = 0; ResultSet rs = null; Vector result = null; PreparedStatement ps = null; try { checkConnection(); String countsql = SQLFilter.getCountSql(sql); ps = connection.prepareStatement(countsql); fillPreparedStatement(ps, objectParams); rs = connection.executePreQuery(); if (rs != null && rs.next()) { total = rs.getLong(1); } if (rs != null) { rs.close(); rs = null; } if (ps!=null) { ps.close(); ps = null; } int totalpages = (int) Math.ceil((double) total / pageSize); if (curPage > totalpages) curPage = totalpages; if (curPage <= 0) curPage = 1; ps = connection.prepareStatement(sql); if (total != 0) connection.setMaxRows(curPage * pageSize); fillPreparedStatement(ps, objectParams); rs = connection.executePreQuery(); if (rs == null) { return ri; } 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 ri; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?