简单的JDBC工具类

对于jdbc的许多操作例如数据库连接和关闭,重复度太多,为了简化代码,可简单的写 JDBC的工具类,
对于数据库连接操作,为提高使用的拓展性,可通过使用配置文件来优化
简单的配置文件如下

url=jdbc:mysql://localhost:3306/ss ?serverTimezone=UTC
user=root
password=123456
Diver=com.mysql.cj.jdbc.Driver
  • jdbc工具类
package hj.jdbc;

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

/**
 * jdbc工具类
 */
public class day2 {
    private  static String url;
    private  static String user;
    private  static String password;
    private  static String diver;
      //写在静态代码块上获取数据库连接并配置文件
    //由于读取该配置文件只需要读取一次,所以用静态代码块更为简化
    static {
        Properties d=new Properties();

        try {
            /*
            动态获取配置文件路径
             */
            Class<day2> day2Class = day2.class;
            ClassLoader classLoader = day2Class.getClassLoader();
            URL resource = classLoader. getResource("jdbc.properties");//URL为统一资源标识符(定位符),可以定位文件的绝对路径
            String path = resource.getPath();//获取字符串路径
          //  System.out.println(path);
            d.load(new FileReader(path));
            url= d.getProperty("url");
             user = d.getProperty("user");
             password = d.getProperty("password");
             diver = d.getProperty("Diver");
            Class.forName(diver);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public  static Connection getconnection() throws  Exception{
    //写在方法上获取数据库连接并配置文件
//        Properties d=new Properties();
//        Class<day2> day2Class = day2.class;
//        ClassLoader classLoader = day2Class.getClassLoader();
//        InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
//
//            d.load(resourceAsStream);
//
//        String url = d.getProperty("url");
//        String user = d.getProperty("user");
//        String password = d.getProperty("password");
//        String diver = d.getProperty("Diver");
//        Class.forName(diver);
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }
    下面为不同条件下的关闭数据库方法
    public static void close(Statement stmt, Connection con, ResultSet s){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } if (con!=null){
                try {
                   con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } if (s!=null){
                    try {
                        s.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
    public static void close2(PreparedStatement stmt, Connection con, ResultSet s){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } if (con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } if (s!=null){
                    try {
                        s.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
    public static void close1(Statement stmt, Connection con){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } if (con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }

    }
    public static void close3(PreparedStatement stmt, Connection con){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } if (con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }

    }


    public static void main(String[] args) {
        Connection getconnection=null;
        Statement getstatement=null;
        ResultSet resultSet=null;
        try {
            getconnection = day2.getconnection();
            getstatement = getconnection.createStatement();
            String sql="select * from s";
             resultSet = getstatement.executeQuery(sql);
            while(resultSet.next()){
                String name = resultSet.getString("name");
                System.out.println(name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            day2.close(getstatement,getconnection,resultSet);
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值