数据库连接池的简单使用--c3p0, dbcp, Druid
C3P0的简单使用
使用步骤
1.导入jar包
在工程下新建libs文件夹–>File–>Project Structure–>Libraries 导入之后,External Libraries 是下面的情况
2.硬编码方式使用C3P0
public class C3P0Test {
@Test
public void testGetConnection() throws Exception {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" );
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT&rewriteBatchedStatements=true" );
cpds.setUser("root");
cpds.setPassword("abc123");
cpds.setInitialPoolSize(10);
Connection conn = cpds.getConnection();
System.out.println(conn);
}
}
3.使用配置文件方式使用C3P0
配置文件内容如下 配置文件名称为 c3p0-config.xml 配置文件名称不可更改
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<!-- This app is massive! -->
<named-config name="intergalactoApp">
<!-- 提供获取连接的4个基本信息 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT</property>
<property name="user">root</property>
<property name="password">abc123</property>
<!-- 进行数据库连接池管理的基本信息 -->
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">100</property>
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">2</property>
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
@Test
public void testGetConnection1() throws Exception{
ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");
Connection conn = cpds.getConnection();
System.out.println(conn);
}
4. C3P0_JDBCUtils 工具类
public class c3p0JDBCUtils {
private static ComboPooledDataSource cpds = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException {
return cpds.getConnection();
}
}
5. c3p0-config.xml 配置文件放置问题
以idea为例,普通java代码,将配置文件放置在src目录下即可(上述代码即为普通java代码) java web项目 ,将配置文件放置在resource目录下,如下:
DBCP的简单使用
1.导入jar包
同上,不过DBCP需要两个jar包,导入成功结果如下
2.硬编码方式使用DBCP
@Test
public void testGetConnection() throws Exception{
BasicDataSource source = new BasicDataSource();
source.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT");
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
source.setUsername("root");
source.setPassword("abc123");
Connection conn = source.getConnection();
System.out.println(conn);
}
3.配置文件方式使用DBCP
配置文件如下 配置文件名称:dbcp.properties
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=abc123
public void testGetConnection1() throws Exception{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("dbcp.properties");
prop.load(fis);
DataSource dataSource = BasicDataSourceFactory.createDataSource(prop);
Connection conn = dataSource.getConnection();
System.out.println(conn);
}
4.DBCP 连接池工具类
public class dbcpJDBCUtils{
private static DataSource dataSource = null;
static{
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("dbcp.properties");
prop.load(fis);
dataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
Connection conn = dataSource.getConnection();
return conn;
}
}
Druid的简单使用
1.导入jar包
导包过程同上,导入完成后如下所示:
2.硬编码方式使用
@Test
public void getConnection() throws Exception{
DruidDataSource source = new DruidDataSource();
source.setUsername("root");
source.setPassword("abc123");
source.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT");
source.setDriverClassName("com.mysql.cj.jdbc.Driver");
DruidPooledConnection conn = source.getConnection();
System.out.println(conn);
}
3.配置文件方式使用
配置文件名为druid.properties 本次为普通java代码,故将配置文件放置在src目录下
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT
driverClassName=com.mysql.cj.jdbc.Driver
username=root
password=abc123
initialSize=8
maxActive=10
@Test
public void getConnection1() throws Exception {
Properties prop = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
prop.load(is);
DataSource source = DruidDataSourceFactory.createDataSource(prop);
Connection conn = source.getConnection();
System.out.println(conn);
}
4.Druid连接池工具类
public class DruidJDBCUtils {
private static DataSource source = null;
static{
try {
Properties prop = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
prop.load(is);
source = DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Connection getConnection() throws Exception{
Connection conn = source.getConnection();
return conn;
}
}
|