1:第一种方式很easy
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3308/database c3p0.user=root c3p0.password=root
文件名称字为:c3p0.properties(放在src文件夹以下)
//程序我写的比較简单。能够用来測试运行配置。
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 { //数据库连接池默认会到classpath下去找数据库的配置。ComboPooledDataSource data = new ComboPooledDataSource(); Connection conn = data.getConnection(); String sql = "select * from table"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); } } }
2:另外一种方式很灵活好用。也很接近于我们平时的hibernate和struts的配置文件方式
并且能够为多个数据源提供服务,提供default-config和named-config两种配置方式 。
当中里面的详细參数可參照c3p0文档(http://www.mchange.com/projects/c3p0/) 比較简单。
依据属性名字差点儿能够看出意思。
<?假设你把c3p0-config.xml放到src文件夹以下,那么你就不须要指定配置文件,它自己主动会找到这个配置文件。即在程序中不用写System.setProperty("com.mchange.v2.c3p0.cfg.xml",System.getProperty("user.dir")+"/config/c3p0-config.xml");这句话。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"); //上面的配置文件 <default-config> //默认情况下 构造函数传空值。会去找默认配置 DataSource data = new ComboPooledDataSource(); //上面的配置文件 <named-config name="database"> //假设构造函数中传值,则找到相应的配置。 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.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); } } }
3:这样的配置是最不经常使用的,比較繁琐,就是把全部配置都写到程序里面来。
可选择性使用
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();