DBCP數據庫連接池的使用

package tk.dong.connectionPool;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class Pool_dbcp {
    // 首先要導入包commons-pool.jar和commons-dbcp-1.4.jar

    // 聲明數據源
    private static DataSource dataSource;
    private static PreparedStatement pstmt;
    private static ResultSet rs;

    static {
        // 將配置文件以輸入流的形式讀入
        InputStream inputStream = Pool_dbcp.class.getClassLoader()
                .getResourceAsStream("dbcp.properties");

        // 創建屬性操作的對象
        Properties properties = new Properties();

        try {
            // 將配置文件讀入
            properties.load(inputStream);
            // 創建基礎的數據源處理工廠類的對象
            BasicDataSourceFactory basicDataSourceFactory = new BasicDataSourceFactory();
            // 通過基礎的數據處理工廠創建出數據源
            dataSource = basicDataSourceFactory.createDataSource(properties);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 創建返回連接對象的方法
    public static Connection getConn() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static void release(ResultSet rs, PreparedStatement pstmt) {
        // 釋放結果集
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 釋放準備語句
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 更新數據的操作增,刪,改要用到的封裝方法
    public static boolean upDate(String sql, Object[] obj) {
        boolean flag = false;

        try {
            // 準備語句的創建,帶有sql命令的對象
            pstmt = getConn().prepareStatement(sql);

            for (int i = 1; i <= obj.length; i++) {
                pstmt.setObject(i, obj[i - 1]);
            }
            int i = pstmt.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            release(rs, pstmt);
        }
        return flag;
    }

    // 進行批量刪除處理
    public static boolean updateBatchDel(String sql, Object[] ids) {
        boolean flag = false;
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < ids.length; i++) {
                pstmt.setObject(1, ids[i]);
                System.out.println(sql + "---------------" + ids[i]);
                pstmt.addBatch();
            }
            int[] num = pstmt.executeBatch(); // 批量執行
            for (int i = 0; i < num.length; i++) {
                if (num[i] == 0) {
                    try {
                        conn.rollback(); // 進行事務回滾
                        return flag;
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
            conn.commit();// 提交事務
            flag = true;
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            release(rs, pstmt);
        }
        return flag;
    }

    // 根據傳入的表的名稱,和每頁數據得到傳入表的所有的頁數
    // tableName:::::操作的數據表名稱
    // pagesize::::::每頁顯示的信息條數
    public static Integer getCountPage(String tableName, Integer pagesize) {
        Integer countPage = 0;
        String sql = "select count(*) as c from " + tableName;
        Connection conn = Pool_dbcp.getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = Pool_dbcp.getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                int countRecord = rs.getInt("c");
                countPage = countRecord % pagesize == 0 ? countRecord
                        / pagesize : countRecord / pagesize + 1;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            Pool_dbcp.release(rs, pstmt);
        }
        return countPage;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章