1: The first method is very simple.
c3p0.driverClass=com.mysql.jdbc.Driverc3p0.jdbcUrl=jdbc:mysql://localhost:3308/databasec3p0.user=rootc3p0.password=root
File Name: c3p0. properties (put under the src directory)
// The program I wrote is relatively simple and can be used to test the execution configuration.
Package jdbc. mysql; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import com. mchange. v2.c3p0. comboPooledDataSource; public class C3P0 {public static void main (String [] args) throws SQLException {// by default, the database connection pool goes down to classpath to find the database configuration. ComboPooledDataSource data = new ComboPooledDataSource (); Connection conn = data. getConnection (); String SQL = "select * from table"; PreparedStatement pstmt = conn. prepareStatement (SQL); ResultSet rs = pstmt.exe cuteQuery (); while (rs. next () {System. out. println (rs. getString (1 ));}}}
2: The second method is very flexible and easy to use. It is also very similar to our usual hibernate and struts configuration file method.
In addition, it can provide services for multiple data sources and provides default-config and named-config configuration methods.
The specific parameters can be referred to c3p0 document (http://www.mchange.com/projects/c3p0/) is relatively simple. According to the attribute name, we can almost see the meaning.
<?xml version="1.0" encoding="UTF-8"?><c3p0-config> <default-config> <property name="user">root</property> <property name="password">1234</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/database?characterEncoding=UTF-8&useOldAliasMetadataBehavior=true</property> <property name="initialPoolSize">2</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">2</property> </default-config> <named-config name="database"> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3308/database?characterEncoding=UTF-8&useOldAliasMetadataBehavior=true</property> <property name="initialPoolSize">2</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">2</property> </named-config></c3p0-config>
Package jdbc. mysql; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource; public class C3p0JDBC {public static void main (String [] args) throws SQLException {System. setProperty ("com. mchange. v2.c3p0. cfg. xml ", System. getProperty ("user. dir ") +"/config/c3p0-config.xml ") ; // The preceding configuration file <default-config> // by default, the constructor transfers null values. The default configuration DataSource data = new ComboPooledDataSource () will be found; // the configuration file <named-config name = "database"> // if the value in the constructor is, find the corresponding configuration. DataSource data1 = new ComboPooledDataSource ("database"); Connection conn = data. getConnection (); Connection conn1 = data1.getConnection (); String SQL = "select * from table"; PreparedStatement pstmt = conn1.prepareStatement (SQL); ResultSet rs = pstmt.exe cuteQuery (); while (rs. next () {System. out. println (rs. getString (1 ));}}}
3: This configuration is the least commonly used and cumbersome. It is to write all the configurations into the program.
Optional
import com.mchange.v2.c3p0.*;........ComboPooledDataSource cpds = new ComboPooledDataSource();cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );cpds.setUser("dbuser"); cpds.setPassword("dbpassword"); cpds.setMaxStatements( 180 ); cpds.close();