c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="mvc"> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8</property> <!-- ?useUnicode=true&characterEncoding=UTF8 --> <property name="acquireIncrement">2</property> <property name="initialPoolSize">5</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">10</property> <property name="maxStatements">20</property> <property name="maxStatementsPerConnection">5</property> </named-config> </c3p0-config>
JdbcUtils
package Utils; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { public static void reaseConnection(Connection connection) { try { if(connection!=null){ connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static DataSource dataSource = null; static{ dataSource = new ComboPooledDataSource("mvc"); } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println("连接失败"); } return null; } }