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

推荐订阅源

博客园 - 三生石上(FineUI控件)
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
B
Blog
N
Netflix TechBlog - Medium
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
M
MIT News - Artificial intelligence
D
DataBreaches.Net
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LINUX DO - 热门话题
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
Vercel News
Vercel News
T
Tenable Blog
Security Latest
Security Latest
C
Check Point Blog
云风的 BLOG
云风的 BLOG
PCI Perspectives
PCI Perspectives
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
雷峰网
雷峰网
IT之家
IT之家
H
Hacker News: Front Page
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Last Week in AI
Last Week in AI
G
Google Developers Blog
Forbes - Security
Forbes - Security
The Register - Security
The Register - Security
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Martin Fowler
Martin Fowler
K
Kaspersky official blog
P
Proofpoint News Feed
T
Threatpost
Google Online Security Blog
Google Online Security Blog
Microsoft Azure Blog
Microsoft Azure 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 或系统集成的场景。