DButil.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.benyun.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.io.File;
  4. import java.sql.*;
  5. @Slf4j
  6. public class DButil {
  7. public static Connection ConnectAccessFile(String dbpath) throws Exception{
  8. File dbFile = new File(dbpath);
  9. if(!dbFile.exists()){
  10. log.error("db数据库文件不存在,请确认路径是否正确:{}", dbpath);
  11. return null;
  12. }else{
  13. log.info("读取数据库文件,路径:{}", dbpath);
  14. }
  15. Connection conn = null;
  16. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");//这个驱动的地址不要改
  17. String dbur1 = "jdbc:ucanaccess:///"+dbpath;
  18. conn = DriverManager.getConnection(dbur1, "","");
  19. log.info("数据库驱动OK");
  20. return conn;
  21. }
  22. public static void close(Connection con) {
  23. try {
  24. if(con!=null) con.close();
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public static void close(PreparedStatement ps) {
  30. try {
  31. if(ps!=null) ps.close();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. public static void close(ResultSet rs) {
  37. try {
  38. if(rs!=null) rs.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }