Data Source C3P0 configuration,
1. Import the jar package (dbutil-> QueryRunner)
2. C3P0Util tool class
Package com. learning. utils; import java. beans. propertyVetoException; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource; public class C3P0Util {private static DataSource dataSource = new ComboPooledDataSource ();/* the configuration file private static combooleddatasource is not used = New ComboPooledDataSource (); static {try {dataSource. setDriverClass ("com. mysql. jdbc. driver "); // loads the jdbc driver dataSource. setJdbcUrl ("jdbc: mysql: // localhost: 3306/book"); dataSource. setUser ("root"); dataSource. setPassword ("root");} catch (PropertyVetoException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} * // obtain the Connection public static Connection getConnection () {tr from the data source Y {return dataSource. getConnection () ;}catch (SQLException e) {throw new RuntimeException ("database Connection failed") ;}// release the public static void release (connection Connection, Statement statement, resultSet resultSet) {if (connection! = Null) {try {// puts unused connection objects back into the connection pool (actually in decorative mode) connection. close ();} catch (SQLException e) {e. printStackTrace ();} connection = null;} if (statement! = Null) {try {statement. close () ;}catch (SQLException e) {e. printStackTrace () ;} statement = null ;}if (resultSet! = Null) {try {resultSet. close () ;}catch (SQLException e) {e. printStackTrace () ;}resultset = null ;}}}
3. configuration file: c3p0-config.xml (name cannot be changed)
<c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/book</property> <property name="user">root</property> <property name="password">root</property> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> <user-overrides user="test-user"> <property name="maxPoolSize">10</property> <property name="minPoolSize">1</property> <property name="maxStatements">0</property> </user-overrides> </default-config> </c3p0-config>