(三)JDBC连接池&DBUtils—C3P0的使用
2017-08-17 18:24
274 查看
(三)JDBC连接池&DBUtils—C3P0的使用
C3P0开源免费的连接池!目前使用他的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,C3P0使用时还需要添加配置文件c3p0-config.xml通过这个配置来加载驱动和获取连接
C3P0配置文件
需要从官网下载平常可以自己收藏一份来使用
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///web08</property> <property name="user">root</property> <property name="password">123456</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="itheima"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///web08</property> <property name="user">root</property> <property name="password">123456</property> </named-config> </c3p0-config> |
具体参数有:
常见配置项:
C3P0提供核心工具类:
ComboPooledDataSource,如果要使用连接池,必须创建该类的实例对象。public class C3P0Utils { private static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima"); public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } } |
public class C3P0Utils { priva 4000 te static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima"); public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } }
public class TestC3P0 { @Test public void testAddUser1() { Connection conn = null; PreparedStatement pstmt = null; try { // 2.从池子中获取连接 conn = C3P0Utils.getConnection(); String sql = "insert into tbl_user values(null,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "德玛"); pstmt.setString(2, "西亚"); int rows = pstmt.executeUpdate(); if (rows > 0) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } catch (Exception e) { throw new RuntimeException(e); } finally { JDBCUtils_V3.release(conn, pstmt, null); } } @Test public void testAddUser() { Connection conn = null; PreparedStatement pstmt = null; // 1.创建C3P0连接池对象 //在ComboPooledDataSource()方法中参数部分是C3P0配置文件里面的 名字 //加载默认的配置 ComboPooledDataSource dataSource=new ComboPooledDataSource(); //加载有名称的配置 //ComboPooledDataSource dataSource1=new ComboPooledDataSource("itheima"); try { // 2.从池子中获取连接 conn = dataSource.getConnection(); String sql = "insert into tbl_user values(null,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "赵信"); pstmt.setString(2, "德邦"); int rows = pstmt.executeUpdate(); if (rows > 0) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } catch (Exception e) { throw new RuntimeException(e); } finally { JDBCUtils_V3.release(conn, pstmt, null); } } }
相关文章推荐
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null' 及can not load org.gjt.mm.mysql.Driver)
- (四)JDBC连接池&DBUtils—DBCP连接池的使用
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT 连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- jsp&servlet和JDBC的综合使用
- [Servlet&JSP] 使用JDBC连接数据库
- c3p0、dbcp、tomcat jdbc pool 连接池区别(推荐使用jdbc pool)
- [置顶] 中国最好的jdbc连接池-------Druid开源连接池的使用
- 使用装饰着设计模式开发自定义jdbc连接池
- c3p0、dbcp、tomcat jdbc pool 连接池区别(推荐使用jdbc pool)
- Day11-JDBC连接池&DBUtils
- 使用 Tomcat 7 新的连接池 —— Tomcat jdbc pool
- JDBC使用Druid作为连接池
- 在JDBC、Spring或者Hibernate中使用proxool连接池
- c3p0、dbcp、tomcat jdbc pool 连接池区别(推荐使用jdbc pool)
- 1701-MySQL-JDBC-连接池使用