Skip to content

JDBCUtils


引入

利用 JDBC 的连接数据库的过程可分为三步:连接、sql 语句、关闭资源,对于连接、关闭资源都是需要的,为了避免代码冗余,提高代码复用性,可以自定义 Utils 工具类完成连接和关闭资源的操作,提高代码编写效率

定义属性

java
public class JDBCUtils {
    // 用户名
    private static String name;
    // 密码
    private static String password;
    // 数据库
    private static String url;
    // 驱动
    private static String driver;
}

初始化

在静态代码块中初始化,利用 property 获取相关信息

注意点

实际开发中常用下面的方式处理

1. 将编译异常转为运行异常

2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便

java
throw new RuntimeException(e);

示例代码

java
public class JDBCUtils {
    // 用户名
    private static String user;
    // 密码
    private static String password;
    // 数据库
    private static String url;
    // 驱动
    private static String driver;

    // 静态代码块完成初始化
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/JDBC/connect.properties"));
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
        } catch (IOException e) {
             /*
                实际开发中常用下面的方式处理
                1. 将编译异常转为运行异常
                2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
             */
            throw new RuntimeException(e);
        }
    }

}

连接 MySQL

java
// 连接 MySQL
public static Connection getConnection() {
    try {
        return DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
        /*
            实际开发中常用下面的方式处理
            1. 将编译异常转为运行异常
            2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
        */
        throw new RuntimeException(e);
    }
}

关闭资源

java
// 关闭资源
public static void close(ResultSet resultSet, Connection connection,Statement statement){
    try {
        if (resultSet != null) {
            resultSet.close();
        }
        if (connection != null) {
            connection.close();
        }
        if (statement != null) {
            statement.close();
        }
    } catch (SQLException e) {
        /*
            实际开发中常用下面的方式处理
            1. 将编译异常转为运行异常
            2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
        */
        throw new RuntimeException(e);
    }
}

完整代码

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

public class JDBCUtils {
    // 用户名
    private static String user;
    // 密码
    private static String password;
    // 数据库
    private static String url;
    // 驱动
    private static String driver;

    // 静态代码块完成初始化
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/JDBC/connect.properties"));
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
        } catch (IOException e) {
             /*
                实际开发中常用下面的方式处理
                1. 将编译异常转为运行异常
                2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
             */
            throw new RuntimeException(e);
        }
    }

    // 连接 MySQL
    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            /*
                实际开发中常用下面的方式处理
                1. 将编译异常转为运行异常
                2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
             */
            throw new RuntimeException(e);
        }
    }

    // 关闭资源
    public static void close(ResultSet resultSet, Connection connection,Statement statement){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            /*
                实际开发中常用下面的方式处理
                1. 将编译异常转为运行异常
                2. 调用者可以选择捕获该异常,也可以选择默认处理该异常,比较方便
             */
            throw new RuntimeException(e);
        }
    }
}

DQL 语句测试

java
public class JDBCUtils_test {
    public static void main(String[] args) {
        // 定义变量
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        // sql 语句
        String sql = "select * from actor";
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                System.out.println("id:" + id + "\t" + "姓名:" + name + "\t" + "性别:" + sex);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(resultSet,connection,preparedStatement);
        }
    }
}

DML 语句测试

java
public class JDBCUtils_test {
    public static void main(String[] args) {
        // 定义变量
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        // sql 语句
        String sql = "insert into actor values (4,'john','男')";
        try {
            // 获取连接
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            int rows = preparedStatement.executeUpdate();
            if (rows > 0){
                System.out.println("执行成功");
            }else {
                System.out.println("执行失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(resultSet,connection,preparedStatement);
        }
    }
}