栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jdbc-连接池

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jdbc-连接池

Java 中常用的连接池有 C3P0、DBCP、Druid 等。我们使用Druid,好处:

  • 它的帮助文档是中文,便于查看
  • 它是经过阿里大量数据的使用后的产品,一定会好用
1,添加依赖

    com.alibaba
    druid
    1.2.8

2,修改连接数据库工具类
@Slf4j
public final class JdbcUtil {
    // 定义连接数据库的几个参数
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    private JdbcUtil() {
    }

    // 加载驱动类,这个加载操作只在项目中执行一次即可,无须返回加载
    static {
        try {
            // 读取db.properties配置文件
            InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(is);
            DRIVER = prop.getProperty("jdbc.driver");
            URL = prop.getProperty("jdbc.url");
            USERNAME = prop.getProperty("jdbc.username");
            PASSWORD = prop.getProperty("jdbc.password");
        } catch (IOException e) {
            log.info("加载配置文件出错,错误信息为:" + e.getMessage());
            throw new RuntimeException("加载配置文件出错,错误信息为:" + e.getMessage());
        }
    }


    
    public static DataSource getDataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(DRIVER);
        ds.setUrl(URL);
        ds.setUsername(USERNAME);
        ds.setPassword(PASSWORD);
        return ds;
    }

    public static Connection getConnection() {
        try {
            return getDataSource().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException("创建连接对象时出错,错误信息为:" + e.getMessage());
        }
    }

    
    public static int update(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            // 2. 创建连接对象
            conn = getConnection();
            // 3. 创建执行SQL语句对象
            pstm = conn.prepareStatement(sql);
            // 4. 设置参数
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            // 5. 执行SQL语句
            return pstm.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            // 6. 释放资源
            destroy(conn, pstm, null);
        }
    }

    
    public static  List query(Class clazz, String sql, Object...params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List entities = null;

        try {
            conn = getConnection();
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            rs = pstm.executeQuery();
            entities = new ArrayList<>();
            // 获取 ResultSet 对象来获取元数据信息
            ResultSetMetaData metaData = rs.getMetaData();
            // 获取SQL语名中字段的个数
            int columnCount = metaData.getColumnCount();
            // 处理结果集
            while (rs.next()) {
                // 通过反射来实例化对象
                //T entity = clazz.newInstance();
                T entity = clazz.getDeclaredConstructor().newInstance();

                for (int i = 1; i <= columnCount; i++) {
                    // getColumnName() 是获取表中的字段名称 SELECT id,name,age FROM xxx
                    //metaData.getColumnName();
                    // getColumnLabel() 是获取SQL中的字段名称 SELECt id,name as n,age FROM xxx
                    // 获取字段的名称
                    String columnLabel = metaData.getColumnLabel(i);
                    // 通过反射获取对象的字段
                    Field field = clazz.getDeclaredField(columnLabel);
                    // 给这个字段设置值
                    field.setAccessible(true);
                    field.set(entity, rs.getObject(columnLabel));
                }
                entities.add(entity);
            }
            return entities;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询多条数据时出错,错误信息为:" + e.getMessage());
        } finally {
            destroy(conn, pstm, rs);
        }
    }

    
    public static  T queryForObject(Class clazz, String sql, Object...params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        T entity = null;

        try {
            conn = getConnection();
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            rs = pstm.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()) {
                if (clazz == Integer.class) {
                    Constructor constructor = clazz.getDeclaredConstructor(int.class);
                    entity = constructor.newInstance(rs.getInt(1));
                } else if (clazz == Long.class) {
                    Constructor constructor = clazz.getDeclaredConstructor(long.class);
                    entity = constructor.newInstance(rs.getLong(1));
                } else {
                    throw new RuntimeException("参数中能传 int 类型或 long 类型。");
                }
            }

            return entity;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("统计查询时出错,错误信息为:" + e.getMessage());
        } finally {
            destroy(conn, pstm, rs);
        }
    }

    
    public static void destroy(Connection conn, PreparedStatement pstm, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.info("释放 ResultSet 对象时出错,错误信息为:" + e.getMessage());
            }
        }
        if (pstm != null) {
            try {
                pstm.close();
            } catch (SQLException e) {
                log.info("释放 PreparedStatement 对象时出错,错误信息为:" + e.getMessage());
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.info("释放 Connection 对象时出错,错误信息为:" + e.getMessage());
            }
        }
    }

}

在这个类中,我们增加了 getDataSource() 方法用于创建连接池对象。然后在 geConection() 方法中从连接池中获取连接对象。而静态代码块中的 加载驱动器类的代码就不再需要,因为这部分的功能已经放到的连接池对象中了。

3,ThreadLocal

JDK1.2 的版本中已经提供了 ThreadLocal对象了,它是为了解决多线程程序并发问题而产生。
这个类提供了以下几个静态方法:

  • get() :用于获取ThreadLocal 中当前线程共享变量的值。
  • set():设置ThreadLocal中当前线程共享的变量的值。
  • remove():移除 ThreadLocal中当前线程共享的变量的值。
  • initialValue:ThreadLocal没有被当前线程赋值时调用当前线程的remove方法后再调用 get 方法时就会返回这个值。
    修改 JdbcUtil 这个工具类,让它支持 ThreadLocal
@Slf4j
public final class JdbcUtil {
    // 数据源连接池对象
    private static DataSource dataSource;
    // ThreadLocal 对象
    private static ThreadLocal threadLocal;

    private JdbcUtil() {
    }

    // 加载驱动类,这个加载操作只在项目中执行一次即可,无须返回加载
    static {
        try {
            // 读取db.properties配置文件
            InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(is);

            // 使用Druid连接池工厂方式
            dataSource = DruidDataSourceFactory.createDataSource(prop);
            threadLocal = new ThreadLocal<>();
        } catch (Exception e) {
            log.info("加载配置文件出错,错误信息为:" + e.getMessage());
            throw new RuntimeException("加载配置文件出错,错误信息为:" + e.getMessage());
        }
    }

    
    public static Connection getConnection() {
        // 从当前线程中获取连接
        Connection conn = threadLocal.get();
        if (conn == null) {
            try {
                // 如果没有就从连接池中去获取
                conn = dataSource.getConnection();
                // 放到ThreadLocal中
                threadLocal.set(conn);
            } catch (SQLException e) {
                throw new RuntimeException("创建连接对象时出错,错误信息为:" + e.getMessage());
            }
        }
        return conn;
    }

    
    public static int update(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try {
            // 2. 创建连接对象
            conn = getConnection();
            // 3. 创建执行SQL语句对象
            pstm = conn.prepareStatement(sql);
            // 4. 设置参数
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            // 5. 执行SQL语句
            return pstm.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            // 6. 释放资源
            destroy(conn, pstm, null);
        }
    }

    
    public static  List query(Class clazz, String sql, Object...params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List entities = null;

        try {
            conn = getConnection();
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            rs = pstm.executeQuery();
            entities = new ArrayList<>();
            // 获取 ResultSet 对象来获取元数据信息
            ResultSetMetaData metaData = rs.getMetaData();
            // 获取SQL语名中字段的个数
            int columnCount = metaData.getColumnCount();
            // 处理结果集
            while (rs.next()) {
                // 通过反射来实例化对象
                //T entity = clazz.newInstance();
                T entity = clazz.getDeclaredConstructor().newInstance();

                for (int i = 1; i <= columnCount; i++) {
                    // getColumnName() 是获取表中的字段名称 SELECT id,name,age FROM xxx
                    //metaData.getColumnName();
                    // getColumnLabel() 是获取SQL中的字段名称 SELECt id,name as n,age FROM xxx
                    // 获取字段的名称
                    String columnLabel = metaData.getColumnLabel(i);
                    // 通过反射获取对象的字段
                    Field field = clazz.getDeclaredField(columnLabel);
                    // 给这个字段设置值
                    field.setAccessible(true);
                    field.set(entity, rs.getObject(columnLabel));
                }
                entities.add(entity);
            }
            return entities;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询多条数据时出错,错误信息为:" + e.getMessage());
        } finally {
            destroy(conn, pstm, rs);
        }
    }

    
    public static  T queryForObject(Class clazz, String sql, Object...params) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        T entity = null;

        try {
            conn = getConnection();
            pstm = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
            rs = pstm.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()) {
                if (clazz == Integer.class) {
                    Constructor constructor = clazz.getDeclaredConstructor(int.class);
                    entity = constructor.newInstance(rs.getInt(1));
                } else if (clazz == Long.class) {
                    Constructor constructor = clazz.getDeclaredConstructor(long.class);
                    entity = constructor.newInstance(rs.getLong(1));
                } else {
                    throw new RuntimeException("参数中能传 int 类型或 long 类型。");
                }
            }

            return entity;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("统计查询时出错,错误信息为:" + e.getMessage());
        } finally {
            destroy(conn, pstm, rs);
        }
    }

    
    public static void destroy(Connection conn, PreparedStatement pstm, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.info("释放 ResultSet 对象时出错,错误信息为:" + e.getMessage());
            }
        }
        if (pstm != null) {
            try {
                pstm.close();
            } catch (SQLException e) {
                log.info("释放 PreparedStatement 对象时出错,错误信息为:" + e.getMessage());
            }
        }
        if (conn != null) {
            try {
                conn.close();
                // 从 ThreadLocal中删除当前连接对象
                threadLocal.remove();
           } catch (SQLException e) {
                log.info("释放 Connection 对象时出错,错误信息为:" + e.getMessage());
            }
        }
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号