1首先在下面地址下载两个jar包,然后导入jar包
c3p0:JDBC DataSources/Resource Pools download | SourceForge.net
2写一个c3p0的配置文件c3p0-config.xml(数据来自B站up:遇见狂神说)
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- c3p0的缺省(默认)配置 如果在代码中"ComboPooledDataSource ds=new ComboPooledDataSource();"这样写就表示使用的是c3p0的缺省(默认)--> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3333/jdbcstudy?useUnicode=true&characterEncoding=utf8&uesSSL=true&serverTimezone=UTC</property> <property name="user">root</property> <property name="password">My123</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <!--如果在代码中"ComboPooledDataSource ds=new ComboPooledDataSource("MySQL");"这样写就表示使用的是name=MySQL --> <named-config name="MySQL"> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3333/jdbcstudy?useUnicode=true&characterEncoding=utf8&uesSSL=true&serverTimezone=UTC</property> <property name="user">root</property> <property name="password">My123</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>
3编写jdbcUtils_c3p0进行连接(相当于之前进行调用的对象jdbcUtils)
package com.haoran.lesson6; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class jdbcUtils_c3p0 { private static ComboPooledDataSource dataSource=null; static { try{ //创建数据源 dataSource = new ComboPooledDataSource();//十分方便,只需要一个语句。 } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } //新建一个release的方法类来实现 释放连接资源 的功能 public static void release(Connection conn, Statement st, ResultSet rs){ if (rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st!=null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
4进行测试
package com.haoran.lesson5; import com.haoran.lesson6.jdbcUtils_c3p0; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Test_c3p0 { public static void main(String[] args) { Connection conn = null; PreparedStatement pst = null; ResultSet rs =null; try { conn= jdbcUtils_c3p0.getConnection(); String sql="SELECT * FROM `users` WHERE id=?"; //预编译SQL,先写SQL,不执行,用占位符? pst=conn.prepareStatement(sql); //手动给参数赋值 pst.setInt(1,2); rs=pst.executeQuery(); if (rs.next()){ System.out.println(rs.getString("NAME")); } } catch (SQLException e) { e.printStackTrace(); }finally { jdbcUtils_c3p0.release(conn,pst,rs); } } }