jdbc——工具类的创建


封装工具类

1 为什么要封装工具类

- 在实际JDBC中的使用中,存在着大量的重复代码:例如连接数据库、关闭数据库这些操作。
- 我们需要把传统的JDBC代码进行重构,抽取出通用的JDBC工具类。以后连接任何数据库、释放资源都可以使用这个工具类。

![](img/07工具类核心思想.png)

2 重用性方案

2.1 方案思想

(1)新建一个工具类DBUtils。

(2)在工具类DBUtils类中封装获取连接、释放资源两个方法。

- 提供public static Connection getConnection(){}方法。
- 提供public static void closeAll(Connection connection,Statement statement,ResultSet resultSet){}方法

2.2 方案代码

```
package cn.bdqn.demo03;

import java.sql.*;

/**
 * 重用性方案
 * 获取连接
 * 释放资源
 */
public class DBUtils {

    static {// 类加载,执行一次!
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 1.获取连接
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java", "root", "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 2.释放资源
    public static void closeAll(Connection connection, Statement statement,
            ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
```

3 跨平台方案

3.1 方案思想

(1)在项目src文件夹下创建db.properties文件,在文件中编写数据库驱动、数据库url、用户名、密码相关数据。

```
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java
username=root
password=123456
```

(2)在工具类DBUtils类中读取db.properties配置文件数据。

```
定义private static final Properties PROPERTIES = new Properties();//读取配置文件的Map

定义static{

    //通过复用本类自带流,读取配置文件中的数据

    InputStream is = DBUtils.class.getResourceAsStream("配置文件路径");

    // 通过prop对象将流中的配置信息分隔成键值对

    PROPERTIES.load(is);

    //通过driverName的键获取对应的值(com.mysql.jdbc.Driver)

    String driverName=PROPERTIES.getProperty("driver");
    //注册驱动
    Class.forName(driverName);
}
```

(3)在工具类DBUtils类中封装获取连接、释放资源两个方法。

- 提供public static Connection getConnection(){}方法。
- 提供public static void closeAll(Connection connection,Statement statement,ResultSet resultSet){}方法

3.2 方案代码

```
package cn.bdqn.demo04;

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

public class DBUtils {
    // //读取配置文件的Map
    private static final Properties PROPERTIES = new Properties();

    static {
        // 通过复用本类自带流,读取配置文件中的数据
        InputStream is = DBUtils.class.getResourceAsStream("/db.properties");

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

    // 获取连接对象
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    PROPERTIES.getProperty("url"),
                    PROPERTIES.getProperty("username"),
                    PROPERTIES.getProperty("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 释放所有资源
    public static void closeAll(Connection connection, Statement statement,
            ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
```

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不愿是过客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值