JAVA C3p0连接池

C3p0连接池

 package com.cmft.cmsd.utils;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * C3p0连接池
 * 
 * @author 
 * @date 2019年6月4日
 */
@Component
public class C3p0Utils {

    private static String url;

    private static String password;

    private static String username;

    private static String linkx001;

    private static String driver;

    private static String MaxPoolSize;

    private static String MinPoolSize;

    private static String InitialPoolSize;

    private static String MaxStatements;

    public static void setCpds(ComboPooledDataSource cpds) {
        C3p0Utils.cpds = cpds;
    }

    private static ComboPooledDataSource cpds;
    /**
     * 静态代码块加载配置文件信息
     */
    static {
        // 存储数据源接口池
        cpds = new ComboPooledDataSource();
        try {
            // 1.通过当前类获取类加载器
            ClassLoader classLoader = C3p0Utils.class.getClassLoader();
            // 2.通过类加载器的方法获得一个输入流
            InputStream is = classLoader.getResourceAsStream("db.properties");
            // 3.创建一个properties对象
            Properties props = new Properties();
            // 4.加载输入流
            props.load(is);
            // 5.获取相关参数的值
            Map<String, String> map = System.getenv();
            //从变量环境中取
            if (!StringUtils.isAllEmpty(map.get("H_DRIVER"), map.get("H_URL"), map.get("H_USERNAME"),
                map.get("H_PASSWORD"), map.get("H_MAXPOOLSIZE"), map.get("H_MINPOOLSIZE"), map.get("H_INITIALPOOLSIZE"),
                map.get("H_MAXSTATEMENTS"))) {
                cpds.setDriverClass(map.get("H_DRIVER"));
                cpds.setJdbcUrl(map.get("H_URL"));
                cpds.setUser(map.get("H_USERNAME"));
                cpds.setPassword(map.get("H_PASSWORD"));
                cpds.setMaxPoolSize(Integer.parseInt(map.get("H_MAXPOOLSIZE")));
                cpds.setMinPoolSize(Integer.parseInt(map.get("H_MINPOOLSIZE")));
                cpds.setInitialPoolSize(Integer.parseInt(map.get("H_INITIALPOOLSIZE")));
                cpds.setMaxStatements(Integer.parseInt(map.get("H_MAXSTATEMENTS")));
            } else {
                cpds.setDriverClass(props.getProperty("driver"));
                cpds.setJdbcUrl(props.getProperty("url"));
                cpds.setUser(props.getProperty("username"));
                cpds.setPassword(props.getProperty("password"));
                cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
                cpds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
                cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
                cpds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取连接方法
     * 
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            /* Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);*/
            conn = cpds.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源方法
     * 
     * @param conn
     * @param pstmt
     * @param rs
     */
    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Connection connection = getConnection();
        System.out.println(connection);

    }
}

db.properties

url=jdbc:impala://ip:21051/default;AuthMech=3
password=2
username=3
driver=com.cloudera.impala.jdbc41.Driver
MaxPoolSize=20
MinPoolSize=2
InitialPoolSize=5
MaxStatements=30
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值