方法一
缺点:需要安装驱动(一开始就是使用这种方式,就是因为服务器驱动一直安装不了,最后使用了第二种方法)优点:速度快
public static Connection getConnection(String accdbPath){ Connection conn = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+accdbPath; Properties properties = new Properties() ; properties.setProperty("charSet", "GB2312") ; conn=DriverManager.getConnection(url,properties); //conn = DriverManager.getConnection(url, "", "");//没有用户名和密码的时候直接为空 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
方法二
缺点:连接速度慢 优点:不需要额外安装驱动
JAR:jackcess-2.1.11.jar;ucanaccess-4.0.4.jar;hsqldb-2.3.1.jar
public static Connection getConnection(String accdbPath){ Connection conn = null; try { File file = new File(accdbPath); String url = "jdbc:ucanaccess://"+file.getPath(); Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); // Properties properties = new Properties() ; // properties.setProperty("charSet", "GB2312") ; conn=DriverManager.getConnection(url); //conn = DriverManager.getConnection(url, "", "");//没有用户名和密码的时候直接为空 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }