c3p0使用记录

时间:2024-10-16 14:03:38
  • 首先要导入c3p0包.c3p0下载解压后,lib目录下有三个包,使用mysql的话,只需要导入c3p0-0.9.5.2.jar,mchange-commons-java-0.2.11.jar.
  • 要连接mysql数据库,需要导入mysql-connector-java-5.1.42-bin.jar.
  • 为了方便的操作数据库链接进行查询,需要导入commons-dbutils-1.6.jar,commons-collections-3.2.2.jar,commons-logging-1.2.jar.
  • 为了使c3p0方便维护,需要在src目录下创建 c3p0-config.xml
    <?xml version="1.0" encoding="UTF-8"?>

    <c3p0-config>
<default-config>
<property name="user">root</property>
<property name="password">root</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/day20</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
</default-config>
</c3p0-config>
  • 使用c3p0
    import javax.sql.DataSource;

    import com.mchange.v2.c3p0.ComboPooledDataSource;

    public class JdbcUtils {
private static ComboPooledDataSource ds;
static {
ds = new ComboPooledDataSource();
} public static DataSource getDataSource(){
return ds;
}
}