Administrator
2023-08-05 0ee44e75029923734744628a8b059fa41ff3ef55
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
77
78
79
80
81
82
83
84
85
package com.ruoyi.test;
 
/**
 * @Description:
 * @ClassName: ReadAccessDatabase
 * @Author: 刘苏义
 * @Date: 2023年07月21日16:00:57
 * @Version: 1.0
 **/
 
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.sql.*;
 
public class ReadAccessDatabase {
 
    public static void main(String[] args) {
 
//        // 多个数据库文件路径
//        String[] dbPaths = {
//                "D:\\mdb\\道路中心线.mdb",
//                "D:\\mdb\\防风林-灌木.mdb",
//                // 添加更多的数据库文件路径
//        };
//
//        // 遍历每个数据库文件
//        for (String dbPath : dbPaths) {
//            readDataFromAccessDatabase(dbPath);
//        }
    }
 
    public static void readDataFromAccessDatabase(String dbPath) {
        try {
            // 加载JDBC驱动
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
 
            // 连接Access数据库
            String url = "jdbc:ucanaccess://" + dbPath + ";encoding=gb2312;";
            Connection connection = DriverManager.getConnection(url);
            // 获取所有表的元数据
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
 
            // 遍历表
            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                if (!tableName.equals("道路中心线")) {
                    continue;
                }
                System.out.println("表名: " + tableName);
 
                // 获取表的字段元数据
 //               ResultSet column = metaData.getColumns(null, null, tableName, null);
                // 遍历字段
//                while (column.next()) {
//                    String columnName = column.getString("COLUMN_NAME");
//                    System.out.println("字段名: " + columnName);
//                }
//                column.close();
                // 执行查询操作
                String sql = "SELECT * FROM " + tableName; // 替换为你要查询的表名
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                // 处理查询结果
                while (resultSet.next()) {
                    // 读取每一行数据
                    String id = resultSet.getString(1); // 根据表中的列名获取数据
                    String name = resultSet.getString("编号");
 
                    // 可以根据具体的表结构继续获取其他字段的数据
 
                    // 在这里处理获取到的数据,例如输出到控制台或保存到集合中
                    System.out.println("ID: " + id + ", Name: " + name);
                }
                // 关闭连接
                resultSet.close();
                statement.close();
            }
            tables.close();
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}