C3P0连接池使用

来源:互联网 发布:迅游加速器mac版 编辑:程序博客网 时间:2024/05/16 08:03

1、需要引入c3p0的包

2、配置文件配置连接池

c3p0的配置方式分为三种,分别是
1.setters一个个地设置各个配置项
2.类路径下提供一个c3p0.properties文件
3.类路径下提供一个c3p0-config.xml文件

c3p0-config.xml实例:

<?xml version="1.0" encoding="UTF-8"?>  <c3p0-config>      <!-- This is default config! -->      <default-config>          <property name="initialPoolSize">10</property>          <property name="maxIdleTime">30</property>          <property name="maxPoolSize">100</property>          <property name="minPoolSize">10</property>          <property name="maxStatements">200</property>      </default-config>      <!-- This is my config for mysql-->      <named-config name="mysql">          <property name="driverClass">com.mysql.jdbc.Driver</property>          <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>          <property name="user">root</property>          <property name="password">199420</property>          <property name="initialPoolSize">10</property>          <property name="maxIdleTime">30</property>          <property name="maxPoolSize">100</property>          <property name="minPoolSize">10</property>          <property name="maxStatements">200</property>      </named-config>      <!-- This is my config for oracle -->      <named-config name="oracle">          <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>          <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>          <property name="user">scott</property>          <property name="password">liang</property>          <property name="initialPoolSize">10</property>          <property name="maxIdleTime">30</property>          <property name="maxPoolSize">100</property>          <property name="minPoolSize">10</property>          <property name="maxStatements">200</property>      </named-config>  </c3p0-config>  

3、java中获取连接

package utils;import java.sql.Connection;  import java.sql.SQLException;  import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3P0Util {    static ComboPooledDataSource cpds=null;      static{          //这里有个优点,写好配置文件,想换数据库,简单          //cpds = new ComboPooledDataSource("oracle");//这是oracle数据库          cpds = new ComboPooledDataSource("mysql");//这是mysql数据库      }      /**      * 获得数据库连接      * @return   Connection      */      public static Connection getConnection(){          try {              return cpds.getConnection();          } catch (SQLException e) {              e.printStackTrace();              return null;          }      }      /**      * 数据库连接关闭操作      * @param conn        */      public static void close(Connection conn){          if(conn!=null){              try {                  conn.close();              } catch (SQLException e) {                  e.printStackTrace();              }          }      }    /**      * 测试      * @param args      */      public static void main(String[] args) {          Connection conn=getConnection();        String sql = "select * from book_info order by score desc";        DBUtil.query(sql, conn);        try {            System.out.println(cpds.getNumConnections());        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }  }