1:第一种方式非常简单
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3308/database c3p0.user=root c3p0.password=root
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)); } } }
<?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)); } } }
可选择性使用
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();
原文地址:http://blog.csdn.net/u012516914/article/details/36903689