八十七

连接池DBCP和C3P0
DBCP

1,src文件下见一个文件夹config,建立一个资源文件。properties,复制配置信息

2,创建一个类,
Properties pro = new Properties();
pro.load(new FileInputStream(“E:/JS/fuxi/servlet/src/config/dbcp.properties”)); // 读取连接池配置信息
DataSource ds = BasicDataSourceFactory.createDataSource(pro);
Connection con = ds.getConnection();
得到链接

3,导包,可以在项目下创建Directory文件(路径),文件名lib,将jar文件复制进去,
然后单击lib文件,选择add as Library将jar文件添加到文件库中

C3P0
1,在工程的src下创建c3p0_config2.xml配置文件
2,配置信息

<?xml version="1.0" encoding="UTF-8" ?>

< c3p0-config>
< default-config>
< property name=“driverClass”>com.mysql.jdbc.Driver</ property>
< property name=“jdbcUrl”>jdbc:mysql://localhost:3306/person</ property>
< property name=“user”>root</ property >
< property name=“password”>root</ property>

< property name=“initialPoolSize”>5</ property>
< property name=“maxPoolSize”>10</ property>
< property name=“checkoutTimeout”>3000</ property>
</ default-config>
</ c3p0-config>

3,在类中连接配置文件
public class C3P0 {
// 链接xml配置文件
private static ComboPooledDataSource ds = new ComboPooledDataSource(“c3p0-config”);
public void get() throws SQLException {
Connection con = ds.getConnection();
if (con != null){
System.out.println(“连接成功”);
}else {
System.out.println(“连接失败”);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值