dbcp下载地址:http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
根据需要下载需要的文件。
BasicDataSource ds = new BasicDataSource(); String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql:///test"; String user = "root"; String pwd = "123456"; ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(user); ds.setPassword(pwd); ds.setInitialSize(3); ds.setMaxActive(3); Connection conn = ds.getConnection();
package cn.zq.util; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; public class DataSourceUtil { private static DataSource ds; static{ InputStream in = null; try{ Properties prop = new Properties(); in = DataSourceUtil.class .getClassLoader() .getResourceAsStream("jdbc.properties"); prop.load(in); ds = BasicDataSourceFactory.createDataSource(prop); }catch(Exception e){ throw new RuntimeException(e.getMessage(), e); }finally{ if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static DataSource getDataSource(){ return ds; } public static Connection getConnection() throws SQLException{ return ds.getConnection(); } }
driverClassName = com.mysql.jdbc.Driver url = jdbc:mysql:///contacts?characterEncoding=UTF8 username = root password = 123456 maxActive=3 initialSize=3
c3po下载地址:http://sourceforge.net/projects/c3p0/
第一步:导入jar包
c3p0-0.9.5-pre6.jar
mchange-commons-java-0.2.6.3.jar
第二步:在classpath路径下,新建一个c3p0-config.xml文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 默认配置,只可以出现一次 --> <default-config> <!-- 连接超时设置30秒 --> <property name="checkoutTimeout">30000</property> <!-- 30秒检查一次connection的空闲 --> <property name="idleConnectionTestPeriod">30</property> <!--初始化的池大小 --> <property name="initialPoolSize">2</property> <!-- 最多的一个connection空闲时间 --> <property name="maxIdleTime">30</property> <!-- 最多可以有多少个连接connection --> <property name="maxPoolSize">10</property> <!-- 最少的池中有几个连接 --> <property name="minPoolSize">2</property> <!-- 批处理的语句 --> <property name="maxStatements">50</property> <!-- 每次增长几个连接 --> <property name="acquireIncrement">3</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"> <![CDATA[jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8]]> </property> <property name="user">root</property> <property name="password">123456</property> </default-config> </c3p0-config>
package cn.zq.util; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceUtil { private static DataSource ds; static{ ds = new ComboPooledDataSource(); } public static DataSource getDataSource(){ return ds; } public static Connection getConnection() throws SQLException{ return ds.getConnection(); } }ComboPooledDataSource默认有3个构造函数:
指定一个命名连接:
<named-config name="contacts"> <property name="checkoutTimeout">1000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">2</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">5</property> <property name="minPoolSize">2</property> <property name="maxStatements">50</property> <property name="acquireIncrement">3</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"> <![CDATA[jdbc:mysql://127.0.0.1:3306/contacts?useUnicode=true&characterEncoding=UTF-8]]> </property> <property name="user">root</property> <property name="password">123456</property> </named-config>在代码中通过命名的方式连接数据库:
ds = new ComboPooledDataSource("contacts");
总结:c3p0比dbcp用起来更为简单,都是通过配置的方式来获取连接。c3p0的功能比dbcp更为强大,实际上c3p0用的比较多。