最近工作中遇到了數據對接問題,其他平臺給我們傳遞access數據,我們這裏進行解析,然後入庫.
1.依賴包有三個
* 1.commons-lang-2.6.jar
* 2.jackcess-2.1.10.jar
* 3.jackcess-encrypt-2.1.4.jar
jar包可以通過 JAR包搜索下載地址
如果是maven,則在pom.xml中添加
<dependencies> <dependency> <groupId>com.healthmarketscience.jackcess</groupId> <artifactId>jackcess</artifactId> <version>2.1.10</version> </dependency> <dependency> <groupId>com.healthmarketscience.jackcess</groupId> <artifactId>jackcess-encrypt</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
2.有了JAR包,之後寫了一個工具類,方便操作
package com.common.utils; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Set; import com.healthmarketscience.jackcess.Column; import com.healthmarketscience.jackcess.Database; import com.healthmarketscience.jackcess.DatabaseBuilder; import com.healthmarketscience.jackcess.Row; import com.healthmarketscience.jackcess.Table; /** * 解析access數據庫工具類 * @author zws * @Date 2019年3月21日 * 依賴 3個jar包 * 1.commons-lang-2.6.jar * 2.jackcess-2.1.10.jar * 3.jackcess-encrypt-2.1.4.jar */ public class AccessUtil { private Database dataBase; // 數據源 private String filePath; // access文件路徑 : 絕對路徑 例如 D:\\ private Set<String> tableNames;// 所有database中的表 /** * 獲取database數據源中的所有表名稱 * @return */ public Set<String> getTableNames() { return tableNames; } public void setTableNames(Database database) throws IOException { try { this.tableNames = this.dataBase.getTableNames(); } catch (Exception e) { closeDataBase(); // 關閉資源連接 throw new IOException(""); } } /** * 獲取數據源 * @return */ public Database getDataBase() { return dataBase; } public AccessUtil(String filePath) throws IOException { super(); this.filePath = filePath; // 路徑賦值 this.setDatabase(); } // 判斷文件是否存在,如果存在,返回 public File getFile() throws IOException{ if(this.filePath != null){ int lastIndex = this.filePath.lastIndexOf("."); if(".mdb".equals(this.filePath.substring(lastIndex).toLowerCase())){ File file = new File(this.filePath.trim()); if(file.exists()){ return file; } }else{ throw new IOException("文件類型錯誤~~~"); } } return null; } /** * 設置Database || 獲取database數據源 * @throws IOException */ public void setDatabase() throws IOException{ File file = this.getFile(); if(file != null){ try { this.dataBase = DatabaseBuilder.open(file); this.setTableNames(this.dataBase); } catch (IOException e) { throw new IOException("打開文件,解析數據源出現異常~~~"+e.getMessage()); } }else{ throw new IOException("資源路徑錯誤~~~"); } } /** * 關閉資源連接 */ public void closeDataBase(){ try { this.dataBase.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 刪除文件 */ public void delFile(){ if(this.filePath != null){ File file = new File(this.filePath.trim()); if(file.exists()){ file.delete(); } } } public static void main(String[] args) throws IOException { AccessUtil accessUtil = new AccessUtil("d:\\1.mdb"); Database dataBase2 = accessUtil.getDataBase(); // 數據源 Set<String> tableNames2 = accessUtil.getTableNames(); // 所有數據源中的表 Table table = dataBase2.getTable("std"); // 獲取表名爲 std的所有數據 for (Row row : table) { List<? extends Column> columns = table.getColumns(); // 獲取所有的行數據 for (Column column : columns) { String key = column.getName(); // 列名 Object value = row.get(key); // 對應值 System.out.println(key+"=="+value); } } accessUtil.closeDataBase(); // 關閉數據源 accessUtil.delFile(); // 刪除文件 } }
趕緊體驗一番吧......