1.导入相应的JARs
mysql驱动jar包:mysql-connector-java-5.1.28-bin.jar
C3P0需要的jar包:c3p0-0.9.2-pre1.jar和mchange-commons-0.2.jar
2.在src下添加C3P0配置文件:c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/grain</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">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>
</c3p0-config>
3.封装为JdbcUtils,用来获取Connection对象,以及开启和关闭事务。有以下接口:
-
Connection getConnection():从c3p0连接池获取Connection对象,所以需要提供c3p0-config.xml配置文件;
-
beginTransaction():为