DBCP
代码实现:
dbcpconfig.properties
#连接设置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true username=root password=123456 #!-- 初始化连接 -- initialSize=10 #最大连接数量 maxActive=50 #!-- 最大空闲连接 -- maxIdle=20 #!-- 最小空闲连接 -- minIdle=5 #!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -- maxWait=60000 #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】 #注意:user 与 password 两个属性会被明确地传递,因此这里不需要包含他们。 connectionProperties=useUnicode=true;characterEncoding=UTF8 #指定由连接池所创建的连接的自动提交(auto-commit)状态。 defaultAutoCommit=true #driver default 指定由连接池所创建的连接的只读(read-only)状态。 #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix) defaultReadOnly= #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。 #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE defaultTransactionIsolation=READ_UNCOMMITTED
JdbcUtils_DBCP
public class JdbcUtils_DBCP { private static DataSource dataSource = null; static{ try{ InputStream in = JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); Properties properties = new Properties(); properties.load(in); //创建数据源 工厂模式 ---> 创建 dataSource = BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } //获得连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); //从数据源中获取连接 } //释放连接资源 public static void release(Connection conn, PreparedStatement ps, ResultSet rs){ if(rs != null){ try { rs.close(); }catch(SQLException e){ e.printStackTrace(); } } if(ps != null){ try { ps.close(); }catch(SQLException e){ e.printStackTrace(); } } if(conn != null){ try { conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
TestDBCP
public class TestDBCP { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils_DBCP.getConnection(); String sql = "select `name`,`password` from users where `id` = ?"; ps = conn.prepareStatement(sql); ps.setInt(1,1); rs = ps.executeQuery(); while (rs.next()){ System.out.println("name=" + rs.getString("name")); System.out.println("password=" +rs.getString("password")); } } catch (SQLException e) { e.printStackTrace(); }finally { JdbcUtils_DBCP.release(conn,ps,rs); } } }
C3P0
需要用到的jar包:c3p0-0.9.1.jar、mchange-commons-java-0.2.15.jar
代码实现:
c3p0-cpconfig.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- c3p0的缺省(默认)配置 如果在代码中“ComboPooledDataSource ds=new ComboPooledDataSource();”这样写就表示使用的是c3p0的缺省(默认) --> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&uesSSL=true&serverTimezone=UTC</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquiredIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <!-- C3P0的命名配置 如果在代码中“ComboPooledDataSource ds = new ComboPooledDataSource("MySQL");”这样写就表示使用的是name是MySQL的数据源 --> <named-config name="MySQL"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&uesSSL=true&serverTimezone=UTC</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquiredIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </named-config> </c3p0-config>
JdbcUtils_C3P0
public class JdbcUtils_C3P0 { private static ComboPooledDataSource dataSource = null; static{ try{ //代码版配置写法(一般不这样用) /*dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(); dataSource.setJdbcUrl(); dataSource.setUser(); dataSource.setPassword(); dataSource.setMaxPoolSize(); dataSource.setMinPoolSize();*/ //配置文件的写法 dataSource = new ComboPooledDataSource("MySQL"); } catch (Exception e) { e.printStackTrace(); } } //获得连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); //从数据源中获取连接 } //释放连接资源 public static void release(Connection conn, PreparedStatement ps, ResultSet rs){ if(rs != null){ try { rs.close(); }catch(SQLException e){ e.printStackTrace(); } } if(ps != null){ try { ps.close(); }catch(SQLException e){ e.printStackTrace(); } } if(conn != null){ try { conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
TestC3P0
public class TestC3P0 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils_C3P0.getConnection(); String sql = "select `name`,`password` from users where `id` = ?"; ps = conn.prepareStatement(sql); ps.setInt(1,1); rs = ps.executeQuery(); while (rs.next()){ System.out.println("name=" + rs.getString("name")); System.out.println("password=" +rs.getString("password")); } } catch (SQLException e) { e.printStackTrace(); }finally { JdbcUtils_C3P0.release(conn,ps,rs); } } }
结论:无论使用什么数据源,本质还是一样的,DataSource接口不会变,方法就不会变