JDBC抽取工具类

JDBC抽取工具类

准备工作:制作一个jdbc.properties 配置文件
说明一下:本人采用的mysql 是8 版本的

废话少说上码
jdbc.properties 文件

Driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC
user = root
password = cmsxgh

JDBC工具类

/*
将重复的代码块封装成一个静态的类
 */

import java.io.FileReader;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCutil {
     
    private static String Driver;
    private static String url;
    private static String user;
    private static String password;

    //静态代码块只会随着类的加载而加载
    static {
     
        try {
     
            Properties pro = new Properties();
            ClassLoader classLoader = JDBCutil.class.getClassLoader();
            URL re = classLoader.getResource("jdbc.properties");
            String path = re.getPath();
            System.out.println(path);

            pro.load(new FileReader(path));
            Driver = pro.getProperty("Driver");
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");

//            注册驱动
            Class.forName(Driver);

        } catch (Exception e) {
     
            e.printStackTrace();
        }

    }

    //获取连接对象
    public static Connection getConnection() throws SQLException {
     

        return DriverManager.getConnection(url,user,password);
    }

    //释放资源
    public static void close(Statement stat, Connection conn){
     
        if (stat != null){
     
            try {
     
                stat.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
        if (conn != null){
     
            try {
     
                conn.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
    }
    //通过重载的方法来实现
    public static void close(ResultSet res, Statement stat, Connection conn){
     
        if (res != null){
     
            try {
     
                res.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
        if (stat != null){
     
            try {
     
                stat.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
        if (conn != null){
     
            try {
     
                conn.close();
            } catch (SQLException e) {
     
                e.printStackTrace();
            }
        }
    }
}

测试此工具类 来简化代码的重复性

package com.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_1 {
     
    public static void main(String[] args) {
     
        Connection conn = null;
        Statement stat = null;

        try {
     
            //获取连接驱动
            conn = JDBCutil.getConnection();

            //编写执行sql语句
            String sql = "update student set math = 110 where id = 1";

            //执行sql语句
            stat = conn.createStatement();
            int i = stat.executeUpdate(sql);
            if (i>0){
     
                System.out.println("修改成功");
            }else {
     
                System.out.println("修改失败");
            }

        } catch (SQLException throwables) {
     
            throwables.printStackTrace();
        }finally {
     
            //释放资源
            JDBCutil.close(stat,conn);
        }
    }
}

运行结果为:
JDBC抽取工具类_第1张图片
ok 以上就是jdbc抽取的工具类来实现代码的简化。

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