数据库连接池C3P0和Driud基础使用

数据库的连接用一次就扔,下次要用时再创建,这样很浪费资源。

要实现多次复用,在为一个用户提供完服务之后可以去为下一位用户提供服务

这就需要用到数据库连接池技术:初始化时就生成n个连接,每有一个用户要使用就分配出去一个。使用完毕后,调用close进行回收。有最大数目限制

好用的数据库连接池技术:C3P0()、druid(阿里)

C3P0使用:

1、官网下载:https://sourceforge.net/projects/c3p0/files/latest/download

2、导入核心与依赖包:c3p0-0.9.5.5.jar、mchange-commons-java-0.2.19.jar,原本的数据库jdbc不要忘记导

3、导入xml格式的配置信息到src下:c3p0-config.xml。注意名称不要错。配置信息分为默认模式和另一个模式。记得修改其中参数

4、获取连接池对象:

DataSource ds = new ComboPooledDataSource();

不传递参数则采用默认配置。传入字符串参数则会查找该名称的配置信息

5、获取连接:

Connection connection = ds.getConnection();

附:格式:

<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test2</property>
<property name="user">root</property>
<property name="password">123456</property>

<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="checkoutTimeout">3000</property>
</default-config>

<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day25</property>
<property name="user">root</property>
<property name="password">root</property>

<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>

druid使用:

1、加载jar包:

druid-1.0.9

2、将配置文件导入src下:

druid.properties

3、加载配置文件信息

Properties properties = new Properties();
InputStream is = Druid_Demo1.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(is);

4、使用配置文件信息创建连接池

DataSource ds = DruidDataSourceFactory.createDataSource(properties);

5、创建连接

Connection connection = ds.getConnection();

 

 druid工具类:

为了方便使用,创建工具类

public class Druid_Utils {
private static DataSource ds = null;
//静态

static {
// 在静态代码块中完成配置信息的导入和数据库连接池的初始化
Properties properties = new Properties();
InputStream is = Druid_Utils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
properties.load(is);
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

// 返回一个连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}

// 重载:释放、归还资源
public static void close(Statement statement,Connection connection){
close(null,statement,connection);
}

public static void close(ResultSet rs, Statement statement, Connection connection){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}

if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}

if(connection != null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}

// 返回数据库连接池对象,有的工具只需要一个数据库连接池,连接是调用自己的方法进行创建的
public static DataSource getDataSource(){
return ds;
}
}

完整代码如上

 

posted @ 2021-12-27 09:06  biingpo  阅读(128)  评论(0)    收藏  举报