读取access工具类-ReadAccessUtil2

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工具类
*
*/
public class ReadAccessUtil2 {

private static Logger logger = LoggerFactory.getLogger(ReadAccessUtil2.class);

/**
* @param fileName
* 文件路径地址 , tableNames 需要解析的表 如果有照片表,放到最后
*
* @return Map key: 表名称,value: 表里面的字段值
*
*/
public static Map<String, List<Map<String, Object>>> resolveAccess(
String fileName,
String[] tableNames) throws Exception {

Database open = DatabaseBuilder.open(new File(fileName));
Map<String, List<Map<String, Object>>> resultMap = new HashMap<String, List<Map<String, Object>>>();
Table table = null;
List<? extends Column> columns = null;
String clumnName = null;
List<Map<String, Object>> listsMap = null;
Map<String, Object> objects = null;

for (String tableName : tableNames) {

table = open.getTable(tableName);

if (null == table) {
throw new RuntimeException("Access文件中不存在" + tableName + "。");
}

columns = table.getColumns();

listsMap = new ArrayList<Map<String, Object>>();

for (Row row : table) {

objects = new HashMap<String, Object>();

for (Column column : columns) {
clumnName = column.getName();
objects.put(clumnName, row.get(clumnName));
}

listsMap.add(objects);
}

resultMap.put(tableName, listsMap);
}

return resultMap;

}

public static void main(
String[] args) {
String fileName = "C:\\Documents and Settings\\XXX.MDB";

try {

Map<String, List<Map<String, Object>>> resolveAccess = ReadAccessUtil2.resolveAccess(fileName, new String[] { "tableName" });
} catch (RuntimeException ex) {
System.out.println("RuntimeException");
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println("Exception");
System.out.println(ex.getMessage());
}
System.out.println("ok...");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值