惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
V
V2EX
The GitHub Blog
The GitHub Blog
T
Threatpost
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
A
Arctic Wolf
Google DeepMind News
Google DeepMind News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
W
WeLiveSecurity
Cloudbric
Cloudbric
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
U
Unit 42
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Spread Privacy
Spread Privacy
Hugging Face - Blog
Hugging Face - Blog
O
OpenAI News
N
News and Events Feed by Topic
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
博客园 - 聂微东
V
Visual Studio Blog
美团技术团队
Security Archives - TechRepublic
Security Archives - TechRepublic
G
GRAHAM CLULEY
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
Know Your Adversary
Know Your Adversary
小众软件
小众软件
Engineering at Meta
Engineering at Meta
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
T
Troy Hunt's Blog

博客园 - dcrenl

解决CapabilityAccessManager.db-wal 文件过大 windows 版本nginx报错:maximum number of descriptors supported by select() is 1024 while 删除多个网关 dot net6 在win7上运行报错 远程重启服务器 sqlserver 20008 R2 关闭TLS1.0无法启动服务的解决办法 rust 模块和引用 nohup java按天输出日志 idea 社区版本创建android原生项目 解决win11输入法自定义短语有多个当前日期只有最后一个生效 postgresql使用for循环 Win10打开IE自动跳转至Edge解决办法 身份认证与授权 pnpm : 无法加载文件 \AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本。 - dcrenl C# 获取时间戳 win11 输入法自定义短语输出日期时间变量 SM系列国密算法 Opera打不开网页解决办法 excel 数字转中文大写金额 nginx http跳转到https
Java 应用程序在linux环境中读取和写入 Microsoft Access 数据库(包括 .mdb 和 .accdb 格式)的开源库
dcrenl · 2026-02-11 · via 博客园 - dcrenl

com.healthmarketscience.jackcess 是一个用于在 Java 应用程序中读取和写入 Microsoft Access 数据库(包括 .mdb.accdb 格式)的开源库。它不依赖于 Microsoft Office 或任何本地驱动程序,提供纯 Java 实现,便于集成到跨平台应用中。

主要功能与特性

  • 跨平台支持:可在任何支持 Java 的操作系统上运行,无需安装 Access 或 ODBC 驱动。
  • 支持版本广泛:兼容从 Access 2000 到 Access 2019 的数据库文件格式。
  • 无 GUI:作为一个底层工具库,专注于程序化操作,不提供图形界面。
  • 灵活的数据操作:支持读取、写入、创建表、修改结构等操作,并可通过自定义过滤器控制数据导出。
  • 开源协议:遵循 Apache License 2.0,可免费用于商业和非商业项目。

核心类与使用示例

  1. 打开数据库
    使用 DatabaseBuilder 打开或创建数据库文件:
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import java.io.File;

try (Database db = DatabaseBuilder.open(new File("path/to/your/database.accdb"))) {
    // 数据库操作在此处进行
} catch (IOException e) {
    e.printStackTrace();
}
  1. 读取表数据
    通过 Table 对象遍历记录:
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.Row;

Table table = db.getTable("TableName");
for (Row row : table) {
    System.out.println("ID: " + row.get("ID") + ", Name: " + row.get("Name"));
}
  1. 写入数据
    向表中添加新行:
Object[] rowData = {1, "Alice", 25};
table.addRow(rowData);
  1. 创建新表
    使用 ColumnBuilder 定义字段结构:
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.SqlType;

Table newTable = db.createTable("NewTable",
    ColumnBuilder.UUID("id"),
    ColumnBuilder.text("name", 255),
    ColumnBuilder.number("age", SqlType.INTEGER)
);
  1. 导出特定列(自定义过滤)
    通过继承 SimpleExportFilter 实现列筛选:
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.util.ExportFilter;
import com.healthmarketscience.jackcess.util.ExportUtil;
import java.util.ArrayList;
import java.util.List;

ExportFilter filter = new ExportFilter() {
    private List<Column> cols = new ArrayList<>();
    private int idx = 0;

    @Override
    public List<Column> filterColumns(List<Column> columns) {
        for (Column col : columns) {
            if (idx++ < 3) cols.add(col); // 只导出前3列
        }
        return cols;
    }
};

ExportUtil.exportFile(db, "TableName", new File("output.csv"), true, ",", filter);

Maven 依赖配置

pom.xml 中添加以下依赖(推荐使用最新稳定版,如 3.0.1):

<dependency>
    <groupId>com.healthmarketscience.jackcess</groupId>
    <artifactId>jackcess</artifactId>
    <version>3.0.1</version>
</dependency>

注意:部分旧版本可能依赖 commons-lang,而新版本(如 3.x)已迁移到 commons-lang3,请确保依赖兼容性。

注意事项

  • 表名和字段名区分大小写,需与 Access 中实际名称完全一致。
  • 对于加密的 Access 数据库(如密码保护),需额外引入 jackcess-encrypt 依赖。
  • 建议使用 try-with-resources 语句自动关闭数据库连接,避免资源泄漏。

该库是 Java 开发中处理 Access 数据库的主流选择,尤其适用于需要在无 Office 环境下进行数据迁移、ETL 或系统集成的场景。