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(); 
 | 
        } 
 | 
    } 
 | 
} 
 |