Java 中连接到 Microsoft Access 2007 数据库的正确方法是什么?

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

What's the right way in Java to connect to a Microsoft Access 2007 database?

javajdbcms-access-2007

提问by Leonardo

I'm trying to create a simple connection using the jdbc-odbc bridge:

我正在尝试使用 jdbc-odbc 桥创建一个简单的连接:

public static Connection  getConnection() {
    Connection con =null;
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
            "c:\myfolder\accesdbfile.accdb";
        con = DriverManager.getConnection(conStr);
    } catch(Exception e) {
        e.printStackTrace();}
    return con;
}

But then I get this exception:

但后来我得到了这个例外:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0xa4 Thread 0xec0 DBC 0x2f8574c                                                              Jet'.

Any ideas?

有任何想法吗?

Update 24/Mar/2009: Now it's working. Created a User Data Source, and for some reason the exception went away.

2009 年 3 月 24 日更新:现在可以使用了。创建了一个用户数据源,由于某种原因,异常消失了。

As a general question, What would be the best way to handle database connections in Java?

作为一个普遍的问题,在 Java 中处理数据库连接的最佳方法是什么?

回答by Rob Di Marco

In general, the best way to work with an RDBMS in Java is by using a JDBC driver that is designed to connect directly to the database. Using the JDBC-ODBC bridge tends to be sloww.

通常,在 Java 中使用 RDBMS 的最佳方法是使用旨在直接连接到数据库的 JDBC 驱动程序。使用 JDBC-ODBC 桥往往很慢。

If you are trying to do basic read/write manipulations with an Access database, I would also recommend taking a look at the Hymancesslibrary.

如果您尝试对 Access 数据库进行基本的读/写操作,我还建议您查看Hymancess库。

回答by Vincent Ramdhanie

To answer your general question I would say the best way to handle database connections in Java is to avoid the JDBC-ODBC bridge. Its OK for testing or learning about JDBC but not for real production use. Also, if you have a data source that does not have its own JDBC driver but it has an ODBC driver then you may not have a choice.

要回答您的一般问题,我会说在 Java 中处理数据库连接的最佳方法是避免使用 JDBC-ODBC 桥。它可以用于测试或学习 JDBC,但不适用于实际的生产用途。此外,如果您的数据源没有自己的 JDBC 驱动程序,但它有一个 ODBC 驱动程序,那么您可能别无选择。

The main reason why I suggest that you stay away from it though is that it makes it difficult to deploy your application. You have to set up the Data Source on the machine that you are running your application from. If you have access to the machine no problem, but suppose you are sending the application off to the client? The pure Java JDBC driver works better for this because it is included as part of your application so once your application is installed it is ready to connect to the data source.

我建议您远离它的主要原因是它使您的应用程序难以部署。您必须在运行应用程序的机器上设置数据源。如果您可以访问机器没问题,但假设您将应用程序发送给客户端?纯 Java JDBC 驱动程序在这方面效果更好,因为它作为应用程序的一部分包含在内,因此一旦安装了应用程序,它就可以连接到数据源。

Of course depending on your requirements there are two other types of drivers but thats another discussion.

当然,根据您的要求,还有另外两种类型的驱动程序,但那是另一个讨论。

回答by user3043367

  1. Go to control panel -- > Administrative tool --> ODBC Data Source Administrator
  2. Add database --> Select "Microsoft Driver(*.mdb, *.accdb)"
  3. Dobule click on new database --> Under "Database" click on "select" --> Select your *.accdb file which you hv created as MS access database.
  4. Say OK and go to your java code
  5. Use: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:filename");
  1. 转到控制面板--> 管理工具--> ODBC 数据源管理器
  2. 添加数据库 --> 选择“Microsoft Driver(*.mdb, *.accdb)”
  3. 双击新数据库 --> 在“数据库”下单击“选择” --> 选择您创建的 *.accdb 文件作为 MS Access 数据库。
  4. 说 OK 并转到您的 Java 代码
  5. 使用:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:filename");

It will surely resolve all your problem.

它一定会解决你所有的问题。