一、串連資料庫的綜合類
[java] view plain copy print ? package com.itjh.javaUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.dbcp.ConnectionFactory; import org.apache.commons.dbcp.DriverManagerConnectionFactory; import org.apache.commons.dbcp.PoolableConnectionFactory; import org.apache.commons.dbcp.PoolingDriver; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.pool.ObjectPool; import org.apache.commons.pool.impl.GenericObjectPool; /** * 串連資料庫的綜合類。</br> * 依賴jar包:commons.dbcp-1.4,commons.dbutils-1.3,commons.pool-1.5.4包。 * * @author 宋立君 * @date 2014年07月03日 */ public class DBUtil { private String dri = null; private String url = null; private String username = null; private String password = null; private String poolName = null; // 串連池名稱 private ObjectPool connectionPool = null; // 串連池 // 對應的定時查詢類 private QueryThread queryThread = null; /** * 功能:建構函式 * * @author 宋立君 * @date 2014年07月03日 * @param dri * 驅動全類名,例如:com.mysql.jdbc.Driver。 * @param url * 資料庫url串連,例如: * "jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8" * @param userName * 資料庫使用者名稱,例如:root * @param password * 資料庫密碼,例如:abc * @param poolName * 建立的資料庫連接池的名稱,例如mypool,注意一個web容器此名稱不能重複。 */ public DBUtil(String dri, String url, String userName, String password, String poolName) { this.dri = dri; this.url = url; this.username = userName; this.password = password; this.poolName = poolName; } /** * 執行sql。 * * @param conn * 串連 * @param pstm * PreparedStatement * @return int 執行sql對應的影響行。 * @throws SQLException * @author 宋立君 * @date 2014年07月03日 */ public int execute(Connection conn, PreparedStatement pstm) throws SQLException { try { return pstm.executeUpdate(); } finally { Close(conn); } } /** * 查詢sql。 * * @param conn * 串連 * @param pstm * PreparedStatement * @return List<Map<String,Object>> 查詢的結果集 * @throws SQLException * @author 宋立君 * @date 2014年07月03日 */ public List<Map<String, Object>> query(Connection conn, PreparedStatement pstm) throws SQLException { try { return resultSetToList(pstm.executeQuery()); } finally { Close(conn); } } /** * 功能:ResultSet 轉為List<Map<String,Object>> * * * @param rs * ResultSet 未經處理資料集 * @return List<Map<String,Object>> * @throws java.sql.SQLException * @author 宋立君 * @date 2014年07月03日 */ private List<Map<String, Object>> resultSetToList(ResultSet rs) throws java.sql.SQLException { if (rs == null) return Collections.EMPTY_LIST; ResultSetMetaData md = rs.getMetaData(); // 得到結果集(rs)的結構資訊,比如欄位數、欄位名等 int columnCount = md.getColumnCount(); // 返回此 ResultSet 對象中的列數 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> rowData = new HashMap<String, Object>(); while (rs.next()) { rowData = new HashMap<String, Object>(columnCount); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), rs.getObject(i)); } list.add(rowData); } return list; } /** * 查詢sql語句。 * * @param sql * 被執行的sql語句 * @return List<Map<String,Object>> * @throws SQLException * @author 宋立君 * @date 2014年07月03日 */ public List<Map<String, Object>> query(String sql) throws SQLException { List<Map<String, Object>> results = null; Connection conn = null; try { conn = getConnection(); QueryRunner qr = new QueryRunner(); results = qr.query(conn, sql, new MapListHandler()); } finally { Close(conn); } return results; } /** * 根據參數查詢sql語句 * * @param sql * sql語句 * @param param * 參數 * @return List<Map<String,Object>> * @throws SQLException * @author 宋立君 * @date 2014年07月03日 */ public List<Map<String, Object>> query(String sql, Object param) throws SQLException { List<Map<String, Object>> results = null; Connection conn = null; try { conn = getConnection(); QueryRunner qr = new QueryRunner(); results = (List<Map<String, Object>>) qr.query(conn, sql, param, new MapListHandler()); } catch (SQLException e) { e.printStackTrace(); } finally { Close(conn); } return results; } /** * 執行sql語句 * * @param sql * 被執行的sql語句 * @return 受影響的行 * @throws Exception * @author 宋立君 * @date 2014年07月03日 */ public int execute(String sql) throws Exception { Connection conn = getConnection(); int rows = 0; try { QueryRunner qr = new QueryRunner(); rows = qr.update(conn, sql); } finally { Close(conn); } return rows; }