c3p0 通过c3p0.properties配置文件 连接数据库

[color=blue][size=x-large]C3P0连接说明文档:[url]http://www.mchange.com/projects/c3p0/#what_is[/url][/size][/color]
[color=red]
[size=medium]使用需要2个包:c3p0-****.jar 和mchange-commons-java-**.jar[/size]
[color=green]
c3p0.properties 文件
c3p0.JDBC.url=jdbc:mysql://localhost:3306/ms_cms?characterEncoding=utf8
c3p0.DriverClass=com.mysql.jdbc.Driver
c3p0.user=root
c3p0.pwd=

c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5
[/color]

public Connection dd() throws FileNotFoundException, IOException, PropertyVetoException, SQLException{

Properties pr = new Properties();
//获得src下的c3p0.properties的路径
String c3p0Properties = this.getClass().getClassLoader().getResource("c3p0.properties").getPath();
//路径的编码是UTF-8
c3p0Properties = URLDecoder.decode(c3p0Properties, "utf-8");
//得到文件c3p0.properties文件
java.io.File c3p0File = new java.io.File(c3p0Properties);
//读取c3p0文件的内容
pr.load(new FileInputStream(c3p0File));

// pr.save(new FileOutputStream(c3p0File), null);
//使用c3p0操作数据库
ComboPooledDataSource cpds = new ComboPooledDataSource();
//加载数据驱动
cpds.setDriverClass(pr.getProperty("c3p0DriverClass"));
//连接特定的数据库
cpds.setJdbcUrl(pr.getProperty("c3p0.JDBC.url"));
//数据库用户名
cpds.setUser(pr.getProperty("c3p0.user"));
//数据库用户密码
cpds.setPassword(pr.getProperty("c3p0.pwd"));
//获得连接
Connection conn = cpds.getConnection();
return conn;
}
[/color]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值