C3P0访问数据库工具类

来源:互联网 发布:mac怎么下载单机游戏 编辑:程序博客网 时间:2024/05/24 00:54

创建配置文件:c3p0-config.xml(必须放在src目录下)

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>      <default-config>            <property name="driverClass">com.mysql.jdbc.Driver</property>            <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>            <property name="user">root</property>            <property name="password">123456</property>            <property name="acquireIncrement">5</property>            <property name="initialPoolSize">10</property>            <property name="minPoolSize">5</property>            <property name="maxPoolSize">20</property>      </default-config>      <named-config name="mysql">            <property name="driverClass">com.mysql.jdbc.Driver</property>            <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>            <property name="user">root</property>            <property name="password">123456</property>            <property name="acquireIncrement">5</property>            <property name="initialPoolSize">10</property>            <property name="minPoolSize">5</property>            <property name="maxPoolSize">20</property>      </named-config>      <named-config name="oracle">            <property name="driverClass">com.mysql.jdbc.Driver</property>            <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property>            <property name="user">root</property>            <property name="password">123456</property>            <property name="acquireIncrement">5</property>            <property name="initialPoolSize">10</property>            <property name="minPoolSize">5</property>            <property name="maxPoolSize">20</property>      </named-config></c3p0-config>

创建工具类

public class C3P0Util {      private static DataSource ds = new ComboPooledDataSource();      /**       * 获取链接       * @return       * @throws SQLException       */      public static Connection getConnection() throws SQLException {            return ds.getConnection();      }      /**       * 获取数据源       * @return       */      public static DataSource getDataSource() {            return ds;      }      /**       * 关闭资源       * @param rs       * @param st       * @param con       */      public static void release(ResultSet rs, Statement st, Connection con) {            try {                  if (rs != null) {                        rs.close();                        rs = null;                  }            } catch (SQLException e) {                  e.printStackTrace();            }            try {                  if (st != null) {                        st.close();                        st = null;                  }            } catch (SQLException e) {                  e.printStackTrace();            }            try {                  if (con != null) {                        con.close();                        con = null;                  }            } catch (SQLException e) {                  e.printStackTrace();            }      }}

测试代码

public class C3p0Test {      @Test      public void testAdd(){            Connection con =null;            Statement st =null;            try{                  con=C3P0Util.getConnection();                  st=con.createStatement();                  System.out.println(st);            }catch(Exception e){                  e.printStackTrace();            }finally{                  C3P0Util.release(null, st, con);            }      }}

需要的依赖包

  • c3p0-0.9.1.2
  • c3p0-0.9.1.2-jdk1.3
  • c3p0-oracle-thin-extras-0.9.1.2(兼容oracle)
  • mysql-connector-java-5.0.8-bin
0 0
原创粉丝点击