c3p0快速入门:
1.1.导包(注意:以下两个包是 c3p0包,还有jdbc驱动包没加上去。。)
1.2.添加idea类识别路径
1.3.加载配置文件 c3p0-config.xml ,并放到src文件夹下 (注意:文件名字是固定的,无需修改)
文件内容:
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<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">10</property>
<property name="checkoutTimeout">3000</property>
</default-config>
</c3p0-config>
2.编码
public class JdbcUtil {
public static DataSource ds;
//定义静态区,可只初始化一次区内内容
static {
ds=new ComboPooledDataSource();
}
/**
* 获取数据库连接
* @return Connection 数据库连接
* @throws SQLException
*/
public static Connection getConnect() throws SQLException {
return ds.getConnection();
}
/**
* 有些框架需要传入数据库连接池
* @return DataSource 数据库连接池
*/
public static DataSource getDataSource() {
return ds;
}
}