数据库连接池c3p0配置⽂件
C3P0的使⽤
C3P0中池类是:ComboPooledDataSource。
public void fun1() throws PropertyVetoException, SQLException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.tJdbcUrl("jdbc:mysql://localhost:3306/mydb1");
ds.tUr("root");
ds.tPassword("123");
ds.tDriverClass("sql.jdbc.Driver");
ds.tAcquireIncrement(5) ;
ds.tInitialPoolSize(20) ;
ds.tMinPoolSize(2) ;
ds.tMaxPoolSize(50);
Connection con = ds.getConnection();
System.out.println(con);
con.clo();
}
配置⽂件要求:
⽂件名称:必须叫l
⽂件位置:必须在src下
c3p0也可以指定配置⽂件,⽽且配置⽂件可以是properties,也可以是xml的。当然xml的⾼级⼀些了。但是c3p0的配置⽂件名必须为l,并且必须放在类路径下。
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
<property name="driverClass"&sql.jdbc.Driver</property>
<property name="ur">root</property>
<property name="password">123</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="oracle-config">
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>
<property name="driverClass"&sql.jdbc.Driver</property>
<property name="ur">root</property>
<property name="password">123</property>
<property name="acquireIncrement">3</property>
<property name="initialPoolSize">10</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">2</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
c3p0的配置⽂件中可以配置多个连接信息,可以给每个配置起个名字,这样可以⽅便的通过配置名称来切换配置信息。上⾯⽂件中默认配置为mysql的配置,名为oracle-config的配置也是mysql的配置。
public void fun2() throws PropertyVetoException, SQLException {
ComboPooledDataSource ds = new ComboPooledDataSource();
Connection con = ds.getConnection();
System.out.println(con);
con.clo();
}
public void fun2() throws PropertyVetoException, SQLException {
ComboPooledDataSource ds = new ComboPooledDataSource("orcale-config") ;
Connection con = ds.getConnection();
System.out.println(con);
con.clo();
}