参考

https://www.lagou.com/lgeduarticle/90622.html

https://segmentfault.com/a/1190000022058327

步骤

  1. 项目新建一个 lib 文件夹,将 jar 包放到 lib 下面
image-20201224101503108
  1. pom 中添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- https://mvnrepository.com/artifact/net.sf.ucanaccess/ucanaccess -->
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/ucanaccess-4.0.0.jar</systemPath>
</dependency>

<!-- https://mvnrepository.com/artifact/com.healthmarketscience.jackcess/jackcess -->
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess</artifactId>
<version>2.1.11</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jackcess-2.1.11.jar</systemPath>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/hsqldb-2.5.1.jar</systemPath>
</dependency>

springboot 的打包插件引入一下参数

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- configuration 标签内容为引入代码-->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.bjtcrj.scm.common.utils;

import java.sql.*;
import java.util.*;

public class MdbUtils {

//使用UCanAccess
private final static String JDBC_DRIVER = "net.ucanaccess.jdbc.UcanaccessDriver";
private final static String JDBC_URL = "jdbc:ucanaccess://";


/**
* 查询mdb文件的表数据
* @param absoluteFilePath mdb文件绝对路径
* @param sql 查询的sql语句
* @return
*/
public static List<Map<String, Object>> read(String absoluteFilePath, String sql){

List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码?没有也行(GB2312/GBK)
//prop.put("user", "");
//prop.put("password", "");

String url = JDBC_URL + absoluteFilePath;
//PreparedStatement preparedStatement = null;
Statement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(url, prop);
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

while(resultSet.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i=1; i<= resultSetMetaData.getColumnCount(); i++){
String columnName = resultSetMetaData.getColumnName(i);//列名
Object columnValue = resultSet.getObject(i);
map.put(columnName, columnValue);
}
listMap.add(map);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return listMap;
}

public static void main(String[] args) {
String sql = "select * from houseaddress";
List<Map<String, Object>> listMap = read("/Users/wangwz/Downloads/dao.mdb", sql);
if(listMap != null && listMap.size() > 0){
System.out.println("=====listMap.size()="+listMap.size());
for (Map<String, Object> map : listMap) {
System.out.println(map.toString());
System.out.println("");
}
}
}
}