Java DButils 封装工具类

一、含有的功能

        (1) 获取连接数据库的对象

        (2) 关闭资源

        (3) 开启手动提交事务

        (4) 回滚事务

        (5) 提交事务

package com.bdqn.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtilsPlus {
    // 使用Properties类对象操作properties文件
    private final static Properties PROPERTIES = new Properties();

    //  定义一个ThreadLocal<Connection>对象存放数据库连接对象
    private final static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    // 使用静态代码块注册驱动
    static {
        // 通过复用本类自带流,读取配置文件中的数据
        InputStream is = DBUtilsPlus.class.getResourceAsStream("/jdbc.properties");

        try {
            // 通过prop对象将流中的配置信息分隔成键值对,将配置文件内容加载到properties集合
            PROPERTIES.load(is);
            // 注册驱动,通过driverName的键获取对应的值(com.mysql.jdbc.Driver)
            Class.forName(PROPERTIES.getProperty("driver"));
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接对象的方法
     *
     * @return
     */
    public static Connection getConnection() {
        // 从ThreadLocal对象中获取connection连接对象
        Connection connection = threadLocal.get();
        // 如果为空 则创建
        if (connection == null) {
            try {
                connection = DriverManager.getConnection(
                        PROPERTIES.getProperty("url"),
                        PROPERTIES.getProperty("user"),
                        PROPERTIES.getProperty("password")
                );
                // 将获取的连接对象存放ThreadLocal中
                threadLocal.set(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return connection;
    }


    /**
     * 关闭资源
     *
     * @param resultSet  存放执行SQL语句的结果集合
     * @param connection 连接数据库对象
     * @param statement  发送SQL对象
     */
    public static void closeAll(ResultSet resultSet, Connection connection, Statement statement) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    /**
     * 开启手动提交事务
     */
    public static void startTransaction() {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 提交事务
     */
    public static void commitTransaction() {
        Connection connection = getConnection();
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeAll(null, connection, null);
        }
    }


    /**
     * 回滚事务
     */
    public static void rollbackTransaction() {
        Connection connection = getConnection();
        try {
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeAll(null, connection, null);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 林先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值