Java基础之 连接池 dbcp c3p0

来源:互联网 发布:北师大网络信息门户 编辑:程序博客网 时间:2024/06/05 22:58

dbcp连接池

//dbcp连接池--直接读取dbcp.properties配置文件public Connection dbcpDataSourceByFactory() throws Exception{//1.从配置文件中读取配置参数Properties properties = new Properties();InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");properties.load(in);//2.从BasicDataSourceFactory中获取BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);//3.从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}//dbcp连接池--直接设置参数public Connection dbcpDataSource() throws SQLException{BasicDataSource dataSource = null;//1.创建dbcp数据源实例dataSource = new BasicDataSource();//2.设置必需的参数dataSource.setUsername("root");dataSource.setPassword("123");dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");dataSource.setDriverClassName("com.mysql.jdcb.Driver");//3.设置配置参数//1) 数据库连接池中,初始化连接数的个数dataSource.setInitialSize(5);//2) 最大连接数dataSource.setMaxActive(10);//3) 最小连接数dataSource.setMinIdle(5);//4) 等待数据库连接池分配连接的最长时间dataSource.setMaxActive(10);//从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}

dbcp.properties

username=rootpassword=1230driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///testinitialSize=10maxActive=50minIdle=5maxWait=5000

c3p0链接池
    //c3p0链接池 -- 读取c3p0-config.xml 配置文件    public Connection c3p0DataSourceByXml() throws Exception {        //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例                  ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0");                //2.从数据库连接池中获取链接        Connection conn = cpds.getConnection();        return conn;    }    //c3p0链接池 --- 直接设置参数    public Connection c3p0DataSource() throws Exception {        //1.创建 ComboPooledDataSource 实例        ComboPooledDataSource cpds = new ComboPooledDataSource();                //2.设置参数        cpds.setDriverClass("com.mysql.jdbc.Driver");        cpds.setJdbcUrl("jdbc:mysql:///atguigu");        cpds.setUser("root");        cpds.setPassword("1230");                //3.从数据库连接池中获取链接        Connection conn = cpds.getConnection();        return conn;    }


c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hdc3p0"><!-- 指定连接数据源的基本属性 --><property name="user">root</property><property name="password">1230</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///atguigu</property><!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 --><property name="acquireIncrement">5</property><!-- 初始化数据库连接池时连接的数量 --><property name="initialPoolSize">5</property><!-- 数据库连接池中的最小的数据库连接数 --><property name="minPoolSize">5</property><!-- 数据库连接池中的最大的数据库连接数 --><property name="maxPoolSize">10</property><!-- C3P0 数据库连接池可以维护的 Statement 的个数 --><property name="maxStatements">20</property><!-- 每个连接同时可以使用的 Statement 对象的个数 --><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>


dbcp连接池

//dbcp连接池--直接读取dbcp.properties配置文件public Connection dbcpDataSourceByFactory() throws Exception{//1.从配置文件中读取配置参数Properties properties = new Properties();InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");properties.load(in);//2.从BasicDataSourceFactory中获取BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);//3.从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}//dbcp连接池--直接设置参数public Connection dbcpDataSource() throws SQLException{BasicDataSource dataSource = null;//1.创建dbcp数据源实例dataSource = new BasicDataSource();//2.设置必需的参数dataSource.setUsername("root");dataSource.setPassword("123");dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");dataSource.setDriverClassName("com.mysql.jdcb.Driver");//3.设置配置参数//1) 数据库连接池中,初始化连接数的个数dataSource.setInitialSize(5);//2) 最大连接数dataSource.setMaxActive(10);//3) 最小连接数dataSource.setMinIdle(5);//4) 等待数据库连接池分配连接的最长时间dataSource.setMaxActive(10);//从数据库连接池中获取链接Connection conn = dataSource.getConnection();return conn;}

dbcp.properties

username=rootpassword=1230driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///testinitialSize=10maxActive=50minIdle=5maxWait=5000

c3p0链接池
    //c3p0链接池 -- 读取c3p0-config.xml 配置文件    public Connection c3p0DataSourceByXml() throws Exception {        //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例                  ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0");                //2.从数据库连接池中获取链接        Connection conn = cpds.getConnection();        return conn;    }    //c3p0链接池 --- 直接设置参数    public Connection c3p0DataSource() throws Exception {        //1.创建 ComboPooledDataSource 实例        ComboPooledDataSource cpds = new ComboPooledDataSource();                //2.设置参数        cpds.setDriverClass("com.mysql.jdbc.Driver");        cpds.setJdbcUrl("jdbc:mysql:///atguigu");        cpds.setUser("root");        cpds.setPassword("1230");                //3.从数据库连接池中获取链接        Connection conn = cpds.getConnection();        return conn;    }


c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><named-config name="hdc3p0"><!-- 指定连接数据源的基本属性 --><property name="user">root</property><property name="password">1230</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///atguigu</property><!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 --><property name="acquireIncrement">5</property><!-- 初始化数据库连接池时连接的数量 --><property name="initialPoolSize">5</property><!-- 数据库连接池中的最小的数据库连接数 --><property name="minPoolSize">5</property><!-- 数据库连接池中的最大的数据库连接数 --><property name="maxPoolSize">10</property><!-- C3P0 数据库连接池可以维护的 Statement 的个数 --><property name="maxStatements">20</property><!-- 每个连接同时可以使用的 Statement 对象的个数 --><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>


0 0
原创粉丝点击