- 首先要导入
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>
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;
}
}