`

读取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...");
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics