zoukankan      html  css  js  c++  java
  • 数据库连接池之_c3p0

    C3p0

      1,手动设置参数

     1 @Test
     2     public void demo1(){
     3         Connection connection =null;
     4         PreparedStatement preparedStatement=null;
     5         ResultSet resultSet=null;
     6         
     7         try {
     8             //设置数据库的相关信息
     9             ComboPooledDataSource ds=new ComboPooledDataSource();
    10             ds.setDriverClass("com.mysql.jdbc.Driver");
    11             ds.setJdbcUrl("jdbc:mysql:///day03");
    12             ds.setUser("root");
    13             ds.setPassword("123");
    14             
    15             //获取连接
    16             connection =ds.getConnection();
    17             String sql="select * from Book";
    18             preparedStatement=connection .prepareStatement(sql);
    19             resultSet=preparedStatement.executeQuery();
    20             while(resultSet.next()){
    21                 System.out.println(resultSet.getInt("id")+"  "+resultSet.getString("bname"));
    22             }
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         }finally{
    26             //这个是一个工具类用来关闭连接
    27             JDBCUtils.close(resultSet, preparedStatement, connection);
    28         }
    29     }

      2,自动读取配置文件

     

     1 @Test
     2     public void demo2(){
     3         Connection connection=null;
     4         PreparedStatement ps=null;
     5         ResultSet resultSet=null;
     6         try {
     7             ComboPooledDataSource cs=new ComboPooledDataSource();//可以在这里设置要使用那个数据库
     8             
     9             connection=cs.getConnection();
    10             String sql="insert into Book(bname) values(?)";
    11             ps=connection.prepareStatement(sql);
    12             ps.setString(1,"牧神记");
    13             ps.executeUpdate();
    14             
    15             ps.setString(1,"一念永恒");
    16             int num=ps.executeUpdate();
    17             if(num>0){
    18                 System.out.println("添加成功");
    19             }else{
    20                 System.out.println("添加失败");
    21             }
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }finally{
    25             JDBCUtils.close(ps, connection);
    26         }
    27     }

    c3p0的配置文件名(c3p0-config.xml)

    <?xml version="1.0" encoding="UTF-8"?>
     <c3p0-config> 
         <default-config> 
             <property name="driverClass">com.mysql.jdbc.Driver</property> 
             <property name="jdbcUrl">jdbc:mysql:///day03</property> 
             <property name="user">root</property> 
             <property name="password">123</property> 
             <property name="minPoolSize">5</property> 
             <property name="initialPoolSize">5</property> 
         </default-config>
         
         <named-config name="oracle"> 
              <property name="driverClass">com.mysql.jdbc.Driver</property> 
             <property name="jdbcUrl">jdbc:mysql:///day04</property> 
             <property name="user">root</property> 
             <property name="password">123</property> 
             <property name="minPoolSize">5</property> 
             <property name="initialPoolSize">5</property> 
         </named-config>
    </c3p0-config>
  • 相关阅读:
    3089:爬楼梯
    7592:求最大公约数问题
    JVM中内存回收深入分析,各种垃圾收集器
    PKU 1064 Cable master
    【面试&笔试】ASP.NET的相关问题
    1027. Colors in Mars (20) PAT
    DB_WRITER_PROCESSES与LOG_ARCHIVE_MAX_PROCESSES
    fedora下体验gentoo安装
    一个整数数组里面,除了两个数之外,其他的数字都出现了两次,写一个程序找出这两个数
    [置顶] export命令-linux
  • 原文地址:https://www.cnblogs.com/zhuguangzhe/p/7458239.html
Copyright © 2011-2022 走看看