jdbc工具类封装

JAVA连接JDBC,编写简单的学生管理系统1,发现StudentDao.class的连接SQL和关闭SQL代码过于冗余,所以进行重构。

1.config.properties文件不能直接建在src目录下,这样配置文件会描不到,target文件里面也没有config.properties文件,所以会报空指针异常,config.properties文件应该建在resources目录下),config.properties 输入下列内容,不同的用户设置的user和password会不同

driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/shs_demo?serverTimezone=UTC
user=root
password=root

2.创建工具包类

package JDBC.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class ShsJdbcUtils {
    /**1.工具类不需要new,通过类名称的方法名称访问*/
    private ShsJdbcUtils(){

    }
    /**2.定义工具声明变量*/
    private static String driverClass;
    private static String  url;
    private static String  user;
    private static String  password;
    /** 使用静态代码块,做好声明便于给jdbc变量复制(读取config.properties)*/
    static {
        try {
            //1.读取config.properties的IO路径,路径是指相对路径
            InputStream resourceAsStream= ShsJdbcUtils.class.
                    getResourceAsStream("/config.properties");
            //2.赋值声明好的变量
            Properties properties = new Properties();
            try {
                properties.load(resourceAsStream);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            driverClass = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            //3.注册驱动类
            Class.forName(driverClass);

        }catch (Exception e){
                e.printStackTrace();
        }
    }
    /**
     * 4.封装连接方法
     */
    public static Connection getConnection(){
        try {
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 5.封装释放连接方法
     */
    public static void closeConnection(ResultSet resultSet, Statement statement,
                                       Connection connection){
        try{
            if (resultSet!=null)
                resultSet.close();

            if (statement!=null)
                statement.close();
            if (connection!=null)
                statement.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 增删该---释放jdbc资源(重载)
     */
    public static void closeConnection(Statement statement,Connection connection){
        closeConnection(null,statement,connection);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值