如何从 64 位 Java 连接到 Access .mdb 数据库?

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

How to connect to Access .mdb database from 64-bit Java?

javams-access

提问by GuruKulki

Hi I have the below code to connect to MS Access database on Windows 7 OS. I have changed the Data Source short cut to point to 64bit odbc then 32 bit. But still getting the error as

嗨,我有以下代码可以连接到 Windows 7 操作系统上的 MS Access 数据库。我已将数据源快捷方式更改为指向 64 位 odbc,然后指向 32 位。但仍然收到错误

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at TestDBConnection.main(TestDBConnection.java:21)

And my code is :

我的代码是:

import java.sql.Connection;
import java.sql.DriverManager;

public class TestDBConnection {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try
        {
            System.out.println("filename");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = 
                      "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Test\Tests.mdb";
            Connection conn = DriverManager.getConnection(database, "", "");
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

}

How ever I have SQL Workbench tool through which I can connect to it but not through java code.

我如何拥有 SQL Workbench 工具,通过它我可以连接到它,但不能通过 java 代码连接到它。

Please need help badly as I am struggling with this from past 3 hours searching on Google.

请急需帮助,因为我在过去 3 个小时在 Google 上搜索时一直在为此苦苦挣扎。

回答by Gord Thompson

If your Java app is running in a 64-bit Java Virtual Machine (JVM) then DRIVER={Microsoft Access Driver (*.mdb)}is not going to work because there is no 64-bit version of the Jet database engine. You can...

如果您的 Java 应用程序在 64 位 Java 虚拟机 (JVM) 中运行,那么DRIVER={Microsoft Access Driver (*.mdb)}将无法运行,因为没有 64 位版本的 Jet 数据库引擎。你可以...

  • Download and install the 64-bit version of the Microsoft Access Database Engine from here, and then use DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}in your code.
  • 此处下载并安装 64 位版本的 Microsoft Access 数据库引擎,然后DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}在您的代码中使用。

... or ...

... 或者 ...

  • Run your Java app in a 32-bit JVM and continue to use the existing DRIVER=string. The related answer heremight prove helpful if you choose this option.
  • 在 32 位 JVM 中运行您的 Java 应用程序并继续使用现有DRIVER=字符串。如果您选择此选项,此处的相关答案可能会有所帮助。

... or ...

... 或者 ...

  • Use the UCanAccessJDBC driver for Access databases. It is a free, open-source, pure Java implementation so it works on both 32-bit and 64-bit systems, both Windows and non-Windows. It also works with Java 8 (which has dropped the JDBC-ODBC Bridge). For more details, see:
  • UCanAccessJDBC 驱动程序用于 Access 数据库。它是一个免费的、开源的、纯 Java 实现,因此它适用于 32 位和 64 位系统,包括 Windows 和非 Windows。它也适用于 Java 8(已删除 JDBC-ODBC 桥)。有关更多详细信息,请参阅:

        Manipulating an Access database from Java without ODBC

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

回答by André Schild

You can install the 64 ODBC drivers for Access available from Microsoft

您可以安装 Microsoft 提供的用于 Access 的 64 个 ODBC 驱动程序

http://www.microsoft.com/en-us/download/details.aspx?id=13255

http://www.microsoft.com/en-us/download/details.aspx?id=13255

回答by parth karia

1) you will have to configure System dsn (Driver Microsoft Access Driver(.mdb,.accdb)) 2) link .mdb database in above configuration and write below code.

1) 您必须配置 System dsn (Driver Microsoft Access Driver( .mdb,.accdb)) 2) 在上述配置中链接 .mdb 数据库并编写以下代码。

 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 String database = "jdbc:odbc:systemdsnname";
 Connection conn = DriverManager.getConnection(database, "", "");