您的位置:首页 > 其它

access 数据解析

2019-03-29 21:29 73 查看

最近工作中遇到了数据对接问题,其他平台给我们传递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 tableNames;// 所有database中的表

    /**

    获取database数据源中的所有表名称
  • @return
    */
    public Set 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 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(); // 删除文件
}
}

赶紧体验一番吧…

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: