java基础之JDBC八:Druid连接池的使用

基本使用代码:

/**
 * Druid连接池及简单工具类的使用
 */
public class Test{
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //使用简单工具类从Druid数据库连接池内取出Connection对象
            conn = DruidSimpleUtils.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 {
            DruidSimpleUtils.release(conn, stat, rs);
        }
    }
}

简单工具类:

/**
 * Druid简单工具类
 */
public class DruidSimpleUtils {
    //构造私有

    public DruidSimpleUtils() {
    }

    //定义一个变量 用来记录数据库连接池对象
    private static DataSource ds;

    //由于Druid不会像C3P0那样自动读取配置文件 所以我们使用静态代码块读取配置文件并给数据库连接池赋值
    static {
        Properties pp = new Properties();
        try {
            //加载properties配置文件  这里地址写到src的上一级即可
            pp.load(new FileReader("jdbcTest\src\config.properties"));
            //数据库连接池配置 使用DruidDataSourceFactory工厂来创建Druid连接池
            ds = DruidDataSourceFactory.createDataSource(pp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //后面基本跟C3P0简单工具类基本一致

    /**
     * 获取数据库连接Connection
     * @return
     */
    public static Connection getConnection() {
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 释放资源
    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();
            }
        }
    }
}

config.properties文件:

#zy
#Mon Jun 11 21:15:49 CST 2018
driver=com.mysql.jdbc.Driver
password=root
url=jdbc:mysql:///dbTemp
username=root