package com.ruoyi.test;
|
|
/**
|
* @Description:
|
* @ClassName: ReadAccessDatabase
|
* @Author: 刘苏义
|
* @Date: 2023年07月21日16:00:57
|
* @Version: 1.0
|
**/
|
|
import com.ruoyi.common.utils.ConfigUtils;
|
import com.ruoyi.common.utils.DictUtils;
|
import com.ruoyi.common.utils.file.ImageUtils;
|
import com.sun.imageio.plugins.common.ImageUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.PostConstruct;
|
import java.sql.*;
|
import java.util.Collection;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
@Slf4j
|
@Component
|
public class ReadAccessDatabase {
|
|
public static void main(String[] args) {
|
|
// 多个数据库文件路径
|
String[] dbPaths = {
|
"D:\\Workspaces\\ard\\安瑞达工作资料\\mdb\\道路中心线.mdb"
|
// "D:\\Workspaces\\ard\\安瑞达工作资料\\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;
|
}
|
log.info("表名: " + tableName);
|
|
// 获取表的字段元数据
|
ResultSet column = metaData.getColumns(null, null, tableName, null);
|
// 遍历字段
|
while (column.next()) {
|
String columnName = column.getString("COLUMN_NAME");
|
log.info("字段名: " + 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 no = resultSet.getString("编号");
|
String name = resultSet.getString("道路名称");
|
// 可以根据具体的表结构继续获取其他字段的数据
|
|
// 在这里处理获取到的数据,例如输出到控制台或保存到集合中
|
log.info("ID: " + id + ", No: " + no+ ", Name: " + name);
|
}
|
// 关闭连接
|
resultSet.close();
|
statement.close();
|
}
|
tables.close();
|
connection.close();
|
} catch (ClassNotFoundException | SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
}
|