java编写连接数据库的工具类(DBUtils)

本文介绍了一个用于简化数据库连接操作的工具类,通过连接池技术减少资源浪费,提供了注册驱动、获取连接、释放资源等功能,并实现了增删改查、事务处理等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

连接数据库工具类的编写

1,工具类介绍

本工具类主要对连接数据库的操作进行了简化封装,避免代码的冗余书写,同时也加入了连接池,减少了因为开启和关闭连接带来的资源浪费问题。
主要封装功能有:注册驱动、获取连接、释放资源、(增删改)命令封装、查询命令封装,事务处理等。

2,工具类代码

public class DBUtils_Druid {
    //创建线程池
    private static  DataSource dataSource=null;
    //创建线程局部变量
    private static ThreadLocal<Connection> threadLocal=new ThreadLocal<>();
    //注册驱动
    static{
        Properties properties=new Properties();
        InputStream resource = DBUtils_Druid.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            properties.load(resource);
            dataSource=DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection(){
        try {
            Connection conn=threadLocal.get();
            if(conn==null){
                conn= dataSource.getConnection();
                //对象绑定
                threadLocal.set(conn);
            }

            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //释放资源
    public static void closeAll(ResultSet rs, Statement stat, Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
            if(stat!=null){
                stat.close();
            }
            if(conn!=null){
                if(conn.getAutoCommit()){
                    threadLocal.remove();
                    conn.close();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //执行命令
    public static int executeUpdate(String sql,Object...params){
        PreparedStatement pstat=null;
        Connection conn=null;
        try {
            conn=getConnection();
            pstat= conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstat.setObject(i+1, params[i]);
            }
            return pstat.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException("出现异常");
        }finally {
            closeAll(null,pstat,conn);
        }
    }
    //与事务相关的办法
    //开启事务
    public static void startTransaction() throws SQLException {
        Connection conn=getConnection();
        if(conn!=null){
            conn.setAutoCommit(false);
        }
    }
    //提交事务
    public static void commitTransaction() throws SQLException {
        Connection conn=getConnection();
        if(conn!=null){
            conn.commit();
        }
    }
    //回滚事务
    public static void rollbackTransaction() throws SQLException {
        Connection conn=getConnection();
        if(conn!=null){
            conn.rollback();
        }
    }
    //关闭事务
    public static void closeTransaction() throws SQLException {
        Connection conn=getConnection();
        if(conn!=null){
            //与对象解除绑定
            threadLocal.remove();
            conn.close();
        }
    }
    //封装查询(利用反射+内省实现)
    public static <T> List<T> executeQuery(Class<T> clazz ,String sql,Object...params){
        Connection conn=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        List<T> list=new ArrayList<>();
        try {
            conn=getConnection();
            pstat=conn.prepareStatement(sql);

            if(params!=null){
                for (int i = 0; i < params.length; i++) {
                    pstat.setObject(i+1, params[i]);
                }
            }
            rs=pstat.executeQuery();
            //获取结果集的标题
            ResultSetMetaData metaData = rs.getMetaData();
            System.out.println("标题个数:"+metaData.getColumnCount());
            while (rs.next()){
                //遍历结果集的标题字段
                T obj=clazz.newInstance();//根据反射创建对象
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    String columnLabel = metaData.getColumnLabel(i + 1); //获得每列的字段名称
                    System.out.println(columnLabel);
                    try {
                        //利用内省完成属性赋值
                        PropertyDescriptor pd =new PropertyDescriptor(columnLabel,clazz);
                        if(pd!=null){
                            Method method = pd.getWriteMethod();
                            method.invoke(obj, rs.getObject(columnLabel));
                        }
                    } catch (Exception e) {
                        continue; //字段名出错就继续为下一个字段进行映射
                    }
                }
                System.out.println("-------");
                list.add(obj);
            }
            return list;


        }catch (Exception e){
            throw new BookException("查询失败", e);

        }finally {
        closeAll(rs,pstat,conn);
        }
    }
}

package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值