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对象存放数据库连接对象
    private final static ThreadLocal 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);
        }
    }
}

你可能感兴趣的:(mysql,java)