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=root
password=1230
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
initialSize=10
maxActive=50
minIdle=5
maxWait=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=root
password=1230
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
initialSize=10
maxActive=50
minIdle=5
maxWait=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>