依賴套件下載: http://xiazai.VeVB.COm/201407/tools/java-db-dependency(VeVB.COm).rar
資料庫連接類別源碼:
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.SQL 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.ObjectPoolap; 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 Name = nullString; // 連線池名稱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 *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 {ntry {return pscon.executeUpdate();} finally {Closentry {return pscon.executeUpdate();} finally {Closentry { finally. }/** * 查詢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.MetaData md = rs.MetaData(md) ; // 得到結果集(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 { conn = getConnectionConnection( );int rows = 0;try {QueryRunner qr = new QueryRunner();rows = qr.update(conn, sql);} finally {Close(conn);}return rows;}/** * 執行含參數的sql語句* * @param sql * 被執行的sql語句* @param params * 參數* @return 回傳受影響的行* @throws Exception * @author 宋立君* @date 2014年07月03日*/public int execute(String sql, Object[] params) throws Exception {Connection conn = getConnection();int rows = 0;try {QueryRunner qr = new QueryRunner();rows = qr.update(conn, sql, params); {Close(conn);}return rows;}/** * 關閉連線* * @param conn * @throws SQLException * @author 宋立君* @date 2014年07月03日*/public void Close(Connection conn) throws SQLException {if (conn != null) {conn.close();}DbUtils.closeQuietly(conn); }/** * 啟動連線池* * @author 宋立君* @date 2014年07月03日*/private void StartPool() {try {Class.forName(dri);} catch (ClassNotFoundException e1) {e1.printStackTrace();}if (connectionPool != null) {ShutdownPool();race();}if (connectionPool != null) {ShutdownPool();race} try {connectionPool = new GenericObjectPool(null);ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, "SELECT 1", false,true);Class.forName("org.apache.commons.dbcp.PoolingDriver. = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");driver.registerPool(poolName, poolableConnectionFactory.getPool());} catch (Exception e) {e.printStackTrace();}// 開啟查詢程式queryThread = new QueryThread(this);queryThread.start();}/** * 關閉連線池* * @author宋立君* @date 2014年07月03日*/private void ShutdownPool() {try {PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");driver.closePool(pool); / 關閉定時查詢queryThread.setStartQuery(false);} catch (SQLException e) {e.printStackTrace();}}/** * 得到一個連接* * @return * @author 宋立君* @date 2014年07月03日*/public synchronized Connection getConnection() {Connection conn = null ;try {if (connectionPool == null)StartPool();conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:"+ poolName);} catch (Exception e) {e.printStackTrace();}return conn;}}/** * 當連線池啟動後會自動定時查詢資料庫,防止資料庫連線逾時。 * * @author 宋立君* @date 2014年07月03日*/class QueryThread extends Thread {private DBUtil dbUtil = null;// 是否開啟查詢private boolean startQuery = true;/** * 功能:對應的資料庫連線。 * * @author 宋立君* @date 2014年07月03日* @param dbUtil * 資料庫連線*/public QueryThread(DBUtil dbUtil) {this.dbUtil = dbUtil;}public void run() {while (true) {try {dbUtil = dbUtil;}public void run() {while (true) {try {dbUtil = dbUtil;}public void run() {while (true) {try {dbUtil = dbUtil;}public void run() {while (true) {try {dbUtil = dbUtil;}public void run() {while (true if (startQuery) {this.dbUtil.query("select 1");}// System.out.println(startQuery+" 123");} catch (Exception e) {e.printStackTrace();} finally {try {Thread.sleep(120000);} catch (InterruptedException e) {e.printStackTrace(); }}}}public void setStartQuery(boolean startQuery) {// System.out.println("startQuery shut:"+startQuery);this.startQuery = startQuery;}}