zoukankan      html  css  js  c++  java
  • java基础之JDBC七:C3P0连接池的使用

    使用C3P0的前提是需要引入jar包

    具体使用如下:

    /**
     * c3p0的应用
     * 前提:引入c3p0的jar包
     */
    public class Test {
        public static void main(String[] args) {
            Connection conn = null;
            Statement stat = null;
            ResultSet rs = null;
            try {
                //C3P0会自动读取src下的c3p0-config.xml文件 获取配置信息(driver,url,username,password等)
                //用ComboPooledDataSource这个对象从连接池获取Connection 参数为配置文件的name 可不传则使用默认配置
                ComboPooledDataSource cpds = new ComboPooledDataSource("dbTemp2");
    
                //也可以不使用配置文件 自己设置参数 但是不推荐
                /*
                cpds.setJdbcUrl("");
                cpds.setDriverClass("");
                cpds.setUser("");
                cpds.setPassword("");*/
           //从连接池中获取Connection对象
                conn = cpds.getConnection();
                //后面的代码跟我们之前的代码基本一样
                stat = conn.createStatement();
                String sql = "SELECT * FROM users";
                rs = stat.executeQuery(sql);
                while (rs.next()) {
                    int id = rs.getInt("uid");
                    String name = rs.getString("uname");
                    String psw = rs.getString("psw");
                    System.out.println(id + "--" + name + "--" + psw);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //这里先使用之前工具类的释放资源的方法
                JDBCSimpleUtils.release(conn, stat, rs);
            }
        }
    }

    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:///dbTemp</property>
            <property name="user">root</property>
            <property name="password">root</property>
            <property name="minPoolSize">5</property>
            <property name="initialPoolSize">5</property>
        </default-config>
        <!--name为dbTemp2的配置-->
        <named-config name="dbTemp2">
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql:///dbTemp2</property>
            <property name="user">root</property>
            <property name="password">root</property>
            <property name="minPoolSize">5</property>
            <property name="initialPoolSize">5</property>
        </named-config>
    </c3p0-config>

    百尺竿头 更进一步  抽取C3P0简单工具类来使用:

    /**
     * C3P0简单工具类
     */
    public class C3P0SimpleUtils {
        //1. 构造私有
        private C3P0SimpleUtils() {
        }
    
        //2. 定义一个变量, 用来记录连接池对象. 由于所有的连接池对象都继承自DataSource 所以这里使用多态的形式
        private static DataSource ds = new ComboPooledDataSource();
    
        //3. 对外提供一个方法, 用来获取连接池对象.
    
        /**
         * 获取连接池对象.
         *
         * @return
         */
        public static DataSource getDataSource() {
            return ds;
        }
    
        //4. 对外提供一个方法, 用来获取连接对象. 连接对象是从数据库连接池中获取的.
    
        /**
         * 从数据库连接池获取连接对象
         * @return
         */
        public static Connection getConnection() {
            try {
                return ds.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        //5. 释放资源 释放资源的方法跟之前提取的JDBC简单工具类基本一样
        public static void release(Connection conn, Statement stat, ResultSet rs) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (stat != null) {
                        stat.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void release(Connection conn, Statement stat) {
            try {
                if (stat != null) {
                    stat.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    那么我们使用我们提取出来的工具类来操作一下:

    /**
     * 使用c3p0工具类
     */
    public class Test {
        public static void main(String[] args) {
            Connection conn = null;
            Statement stat = null;
            ResultSet rs = null;
            try {
                //这里使用我们刚抽取出的工具类来从连接池中取出数据库连接对象
                conn = C3P0SimpleUtils.getConnection();
                //下面代码跟之前一样
                stat = conn.createStatement();
                String sql = "SELECT * FROM users";
                rs = stat.executeQuery(sql);
                while (rs.next()) {
                    int id = rs.getInt("uid");
                    String name = rs.getString("uname");
                    String psw = rs.getString("psw");
                    System.out.println(id + "--" + name + "--" + psw);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //这里可以使用我们C3P0SimpleUtils工具类的释放资源的方法
                C3P0SimpleUtils.release(conn, stat, rs);
            }
        }
    }
  • 相关阅读:
    Webpack配置开发环境总结
    vue2.0 引入font-awesome
    vue-cli 脚手架项目简介(一)
    CSS3伪类与伪元素的区别及注意事项
    页面滚动到可视区域执行操作
    56. 合并区间
    <leetcode c++>卖股票系列
    面试题 16.01. 交换数字
    542. 01 矩阵
    <leetcode c++> 445. 两数相加 II
  • 原文地址:https://www.cnblogs.com/blazeZzz/p/9179909.html
Copyright © 2011-2022 走看看