从linux连接到访问数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5742322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:46:29  来源:igfitidea点击:

Connecting to access database from linux

javalinuxms-access

提问by London

I've created my application and tested it under windows, which writes/reads to/from a access DB file.

我已经创建了我的应用程序并在 Windows 下对其进行了测试,该应用程序向/从访问 DB 文件写入/读取。

But in the real world it will be ran in the linux environment, and I have a big issue now, it appears that there are no drivers for linux to access ms acess db, here is how I make the connection now :

但在现实世界中它将在 linux 环境中运行,我现在有一个大问题,似乎没有 linux 访问 ms acess db 的驱动程序,这是我现在建立连接的方法:

private static Connection getConnection() {
        if (connection == null) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + configuration.getAccessDbFile();
                connection = DriverManager.getConnection(conStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

Has anyone encountered something similar to this, does anybody have a suggestion what could I do ?

有没有人遇到过类似的事情,有没有人有建议我该怎么办?

This is the exception I get on the linux :

这是我在 linux 上遇到的异常:

java.lang.NullPointerException
        at sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java:436)
        at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:153)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)

采纳答案by kaliatech

It's uncommon for an application running on Linux to access an MS Access database or use ODBC. ODBC is a windows technology. There are some options for linux, but it's not a common scenario.

在 Linux 上运行的应用程序访问 MS Access 数据库或使用 ODBC 并不常见。ODBC 是一种 Windows 技术。linux 有一些选项,但这不是一个常见的场景。

As you've discovered, there are no Access ODBC drivers on your linux machine, so the JDBC-ODBC bridge fails. You might be able to install a suitable ODBC driver, but I don't know of any that are free or open-source. The defacto ODBC option for linux is:

正如您所发现的,您的 linux 机器上没有 Access ODBC 驱动程序,因此 JDBC-ODBC 桥接失败。您也许可以安装合适的 ODBC 驱动程序,但我不知道有哪些是免费的或开源的。Linux 的事实上的 ODBC 选项是:

A commercial driver for Access based on UnixODBC:

基于UnixODBC的Access商业驱动:

A type 4 JDBC driver that can supposedly connect to Access databases:

可以连接到 Access 数据库的类型 4 JDBC 驱动程序:

I don't have personal experience with any of these. The type 4 JDBC driver would be ideal, but I'd be skeptical that it works as perfectly as advertised.

我对这些都没有个人经验。类型 4 JDBC 驱动程序将是理想的,但我怀疑它是否像宣传的那样完美。

(I am sure you have reasons, but I have to wonder why you are using an Access database if you plan on deploying to Linux machines. I believe the only good reason to have a Java application use an Access database is if it's an existing Access application with custom programming used for purposes beyond the Java application. Otherwise, there are a number of better options. Also consider that if your main reason for using Access is just so you can use Access as a user-friendly GUI tool for forms & reporting, you could still store the data in less restrictive database (Derby, SQLLite, MySQL, PostgreSQL, MS SQL Server, etc) and then connect via ODBC from Access to the database.) This would allow you to deploy your Java application on linux, your database wherever makes most sense, and still use Access to connect to the database from windows. I've done this a number of times.)

(我相信你有理由,但我想知道如果你打算部署到 Linux 机器,你为什么要使用 Access 数据库。我相信让 Java 应用程序使用 Access 数据库的唯一好理由是它是否是现有的 Access具有用于 Java 应用程序以外目的的自定义编程的应用程序。否则,还有许多更好的选择。另外考虑一下,如果您使用 Access 的主要原因只是为了将 Access 作为用户友好的 GUI 工具用于表单和报告,您仍然可以将数据存储在限制较少的数据库(Derby、SQLLite、MySQL、PostgreSQL、MS SQL Server 等)中,然后通过 ODBC 从 Access 连接到数据库。)这将允许您在 linux 上部署 Java 应用程序,您的数据库在最有意义的地方,并且仍然使用 Access 从 Windows 连接到数据库。我已经多次这样做了。)

回答by GuruKulki

Go through thus link which will help you in this.

通过这个链接,这将帮助你。

http://www.neowin.net/forum/topic/887410-java-ms-access-driver-for-maclinux/

http://www.neowin.net/forum/topic/887410-java-ms-access-driver-for-maclinux/

and can you debug and let us know what is the connection string you are passing to get the connection. Because I think you should call getConnection as below

并且您可以调试并让我们知道您为获取连接而传递的连接字符串是什么。因为我认为你应该调用 getConnection 如下

DriverManager.getConnection(conStr, "", "");

回答by jtahlborn

If you are stuck with Access, but your interactions with the database file are pretty "simple" (e.g. you aren't doing complex SQL queries), you could use something like Hymancess. It is a pure-java library which allows you to manipulate an Access database, but it does notprovide a jdbc interface, so you have to write code using the Hymancess api.

如果您坚持使用 Access,但您与数据库文件的交互非常“简单”(例如,您没有进行复杂的 SQL 查询),您可以使用类似Hymancess 的东西。这是一个纯Java库,让您操作Access数据库,但它并没有提供JDBC接口,所以你必须使用Hymancess API编写代码。

(disclaimer, i am a Hymancess developer)

(免责声明,我是一名Hymancess开发人员)

回答by Abel

Use http://Hymancess.sourceforge.net/

使用http://Hymancess.sourceforge.net/

You can read / write a Acceess database from Linux or Windows using Java.

您可以使用 Java 从 Linux 或 Windows 读取/写入 Access 数据库。

回答by Gord Thompson

UCanAccessis a free, open-source pure Java JDBC driver for Access databases. For more information on how to use it, see

UCanAccess是用于 Access 数据库的免费、开源的纯 Java JDBC 驱动程序。有关如何使用它的更多信息,请参阅

Manipulating an Access database from Java without ODBC

在没有 ODBC 的情况下从 Java 操作 Access 数据库