package com.benyun.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.sql.*;

@Slf4j
public class DButil {


    public static Connection ConnectAccessFile(String dbpath) throws Exception{

        File dbFile = new File(dbpath);

        if(!dbFile.exists()){

            log.error("db数据库文件不存在，请确认路径是否正确：{}", dbpath);

            return null;

        }else{

            log.info("读取数据库文件，路径：{}", dbpath);

        }

        Connection conn = null;

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");//这个驱动的地址不要改

        String dbur1 = "jdbc:ucanaccess:///"+dbpath;

        conn = DriverManager.getConnection(dbur1, "","");

        log.info("数据库驱动OK");

        return conn;

    }

    public static void close(Connection con) {

        try {

            if(con!=null) con.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

    public static void close(PreparedStatement ps) {

        try {

            if(ps!=null) ps.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

    public static void close(ResultSet rs) {

        try {

            if(rs!=null) rs.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

}
