DBCP,C3P0连接池

自己创建一个工具类,用于实现数据库的连接和释放

先在src目录下新建一个db.properties文件(一定要在src目录下),这样不用每次加载驱动和建立连接的时候都要写

driver = com.mysql.cj.jdbc.Driver #用于加载驱动
url = jdbc:mysql://localhost:3306/jdbcStudy?useUnicode = true&characterEncoding = utf8&useSSL = true
user = XXX
pwd = XXXXX #自己数据库的用户名和密码

建立工具类

public class JDBCUtils {
    private static String driver = "";
    private static String url = "";
    private static String user = "";
    private static String password = "";

    static {
        try {
            InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"); 

            Properties properties = new Properties();
            properties.load(inputStream); //利用反射读取数据流

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("pwd");

            Class.forName(driver); //加载数据库驱动


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

    public static Connection getConnector () throws SQLException { //建立数据库连接
        return DriverManager.getConnection(url, user, password);
    }

    public static void Release (Connection conn, Statement sta, ResultSet res) throws SQLException { //释放连接
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

但是建立连接都很消耗内存, 利用连接池技术提前准备好连接,用完的连接并不会立刻全部放回池中,而是留一部分等超时后再放入,提高利用率

DBCP连接池

  • 首先要导入两个jar包  commons-dbcp-1.4.jar 和 commons-pool-1.6.jar
  • 在src目录下配置dbcp-config.properties文件
#连接设置
driverClassName = com.mysql.cj.jdbc.Driver #这个名字不能自己定义,只能是driverClassName,是数据源中定义好的
url = jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true
username = XXX
password = XXXXX #自己的用户名和密码

#
initialSize = 10

#最大连接数量
maxActive = 50

#
maxIdle = 20

#
minIdle = 5

#
maxWait = 60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties = useUnicode=true;characterEncoding=UTF8

#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit = true

#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly = true

#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation = READ_UNCOMMITTED
  • 修改之前的工具类
public class JDBCUtils_DBCP {
    private static DataSource dataSource = null; 

    static {
        try {
            InputStream inputStream = JDBCUtils_DBCP.class.getClassLoader().getResourceAsStream("DBCPconfig.properties");

            Properties properties = new Properties();
            properties.load(inputStream);

            //创建数据源,工厂模式,createDataSource读取的名字为driverClass,这也是之前说的名字不能改的原因
            dataSource = BasicDataSourceFactory.createDataSource(properties); 


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

    public static Connection getConnector () throws SQLException {
        //数据源自带连接,直接调用DataSourse的getConnection()方法即可,本质和一开始写的源代码没有区别,从数据源获取链接
        return dataSource.getConnection();
    }

    public static void Release (Connection conn, Statement sta, ResultSet res) throws SQLException {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

C3P0连接池 

  • 导入两个jar包mchange-commons-java-0.2.20.jar 和 c3p0-0.9.5.5.jar 
  • 在src目录下配置c3p0-config.xml文件,这里有两个注意点
    1. 一定要配置在src目录下
    2. 名字一定为c3p0-config.xml,不能改成别的


    
    
        com.mysql.cj.jdbc.Driver
        jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
        XXX
        XXXX

        5
        10
        5
        20
    

    
        com.mysql.cj.jdbc.Driver
        jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
        XXX
        XXXX

        5
        10
        5
        20
    

  • 修改工具类
public class JDBCUtils_C3P0 {
    private static ComboPooledDataSource comboPooledDataSource = null;

    static {

        try {
            //不用再去利用反射读取了,xml文件一开始就会加载,直接new ComboPooledDataSource()即可
            comboPooledDataSource = new ComboPooledDataSource(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnector () throws SQLException {
        //调用comboPooledDataSource的getConnection方法,本质还是不变
        return comboPooledDataSource.getConnection(); 
    }
    public static void Release (Connection conn, Statement sta, ResultSet res) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (sta != null) {
            try {
                sta.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(mysql,数据库,database)