在没有 ODBC 的情况下从 Java 操作 Access 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21955256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manipulating an Access database from Java without ODBC
提问by Gord Thompson
I want to manipulate a Microsoft Access database (.accdb or .mdb file) from my Java project. I don't want to use the JDBC-ODBC Bridge and the Access ODBC driver from Microsoft because:
我想从我的 Java 项目操作 Microsoft Access 数据库(.accdb 或 .mdb 文件)。我不想使用 Microsoft 的 JDBC-ODBC Bridge 和 Access ODBC 驱动程序,因为:
- the JDBC-ODBC Bridge has been removed from Java SE 8 and is not supported (ref: here),
- the JDBC-ODBC Bridge does not work properly with the Access ODBC driver when text includes Unicode characters with code points above U+00FF (ref: here), so such a setup would not be able to handle characters such as Greek, Russian, Chinese, Arabic, etc.,
- the Access ODBC driver from Microsoft only works in Windows, and
- there are separate 32-bit and 64-bit versions of the Access Database Engine (and ODBC driver) which can be a nuisance for deployment.
- JDBC-ODBC Bridge 已从 Java SE 8 中删除并且不受支持(参考:此处),
- 当文本包含代码点高于 U+00FF 的 Unicode 字符时,JDBC-ODBC 桥无法与 Access ODBC 驱动程序正常工作(参考:此处),因此此类设置将无法处理希腊语、俄语、中文等字符,阿拉伯语等,
- Microsoft 的 Access ODBC 驱动程序仅适用于 Windows,并且
- 有不同的 32 位和 64 位版本的 Access 数据库引擎(和 ODBC 驱动程序),这对于部署来说可能很麻烦。
I have seen other answers mentioning a JDBC driver for Access databases named UCanAccess. How can I set up my Java project to use this approach?
我看到其他答案提到名为UCanAccess 的Access 数据库的 JDBC 驱动程序。如何设置我的 Java 项目以使用这种方法?
(Answers suggesting better ways of working with Access databases from Java would also be most welcome.)
(建议更好地从 Java 使用 Access 数据库的答案也将是最受欢迎的。)
采纳答案by Gord Thompson
UCanAccessis a pure Java JDBC driver that allows us to read from and write to Access databases without using ODBC. It uses two other packages, Hymancessand HSQLDB, to perform these tasks. The following is a brief overview of how to get it set up.
UCanAccess是一个纯 Java JDBC 驱动程序,它允许我们在不使用 ODBC 的情况下读取和写入 Access 数据库。它使用另外两个包Hymancess和HSQLDB来执行这些任务。以下是如何设置它的简要概述。
Option 1: Using Maven
选项 1:使用 Maven
If your project uses Mavenyou can simply include UCanAccess via the following coordinates:
如果您的项目使用Maven,您可以通过以下坐标简单地包含 UCanAccess:
groupId:net.sf.ucanaccess
artifactId:ucanaccess
groupId:net.sf.ucanaccess
artifactId:ucanaccess
The following is an excerpt from pom.xml
, you may need to update the <version>
to get the most recent release:
以下摘自pom.xml
,您可能需要更新<version>
以获得最新版本:
<dependencies>
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.4</version>
</dependency>
</dependencies>
Option 2: Manually adding the JARs to your project
选项 2:手动将 JAR 添加到您的项目中
As mentioned above, UCanAccess requires Hymancess and HSQLDB. Hymancess in turn has its own dependencies. So to use UCanAccess you will need to include the following components:
如上所述,UCanAccess 需要 Hymancess 和 HSQLDB。Hymancess 反过来也有它自己的依赖关系。因此,要使用 UCanAccess,您需要包含以下组件:
UCanAccess(ucanaccess-x.x.x.jar)
HSQLDB(hsqldb.jar, version 2.2.5 or newer)
Hymancess(Hymancess-2.x.x.jar)
commons-lang(commons-lang-2.6.jar, or newer 2.x version)
commons-logging(commons-logging-1.1.1.jar, or newer 1.x version)
UCanAccess(ucanaccess-xxxjar)
HSQLDB(hsqldb.jar, version 2.2.5 or newer)
Hymancess(Hymancess-2.xxjar)
commons-lang(commons-lang-2.6.jar, or newer 2.x version)
commons-logging( commons-logging-1.1.1.jar,或更新的1.x 版本)
Fortunately, UCanAccess includes all of the required JAR files in its distribution file. When you unzip it you will see something like
幸运的是,UCanAccess 在其分发文件中包含了所有必需的 JAR 文件。当你解压缩它时,你会看到类似的东西
ucanaccess-4.0.1.jar
/lib/
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
Hymancess-2.1.6.jar
All you need to do is add all five (5)JARs to your project.
您需要做的就是将所有五 (5) 个JAR 添加到您的项目中。
NOTE: Do notadd
loader/ucanload.jar
to your build path if you are adding the other five (5) JAR files. TheUcanloadDriver
class is only used in special circumstances and requires a different setup. See the related answer herefor details.
注意:不要不加
loader/ucanload.jar
,如果要添加其他五(5)JAR文件到您的构建路径。该UcanloadDriver
班仅在特殊情况下使用,需要进行不同的设置。有关详细信息,请参阅此处的相关答案。
Eclipse:Right-click the project in Package Explorer and choose Build Path > Configure Build Path...
. Click the "Add External JARs..." button to add each of the five (5) JARs. When you are finished your Java Build Path should look something like this
Eclipse:在 Package Explorer 中右键单击该项目并选择Build Path > Configure Build Path...
. 单击“添加外部 JAR...”按钮以添加五 (5) 个 JAR 中的每一个。完成后,您的 Java 构建路径应如下所示
NetBeans:Expand the tree view for your project, right-click the "Libraries" folder and choose "Add JAR/Folder...", then browse to the JAR file.
NetBeans:展开项目的树视图,右键单击“Libraries”文件夹并选择“Add JAR/Folder...”,然后浏览到 JAR 文件。
After adding all five (5) JAR files the "Libraries" folder should look something like this:
添加所有五 (5) 个 JAR 文件后,“Libraries”文件夹应如下所示:
IntelliJ IDEA:Choose File > Project Structure...
from the main menu. In the "Libraries" pane click the "Add" (+
) button and add the five (5) JAR files. Once that is done the project should look something like this:
IntelliJ IDEA的:选择File > Project Structure...
主菜单。在“库”窗格中,单击“添加”( +
) 按钮并添加五 (5) 个 JAR 文件。完成后,项目应如下所示:
That's it!
就是这样!
Now "U Can Access" data in .accdb and .mdb files using code like this
现在“你可以访问”数据在 .accdb 和 .mdb 文件中使用这样的代码
// assumes...
// import java.sql.*;
Connection conn=DriverManager.getConnection(
"jdbc:ucanaccess://C:/__tmp/test/zzz.accdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT [LastName] FROM [Clients]");
while (rs.next()) {
System.out.println(rs.getString(1));
}
Disclosure
披露
At the time of writing this Q&A I had no involvement in or affiliation with the UCanAccess project; I just used it. I have since become a contributor to the project.
在撰写本问答时,我没有参与或隶属于 UCanAccess 项目;我刚用过。从那以后,我成为了该项目的贡献者。