Java中连接到Microsoft Access 2007数据库的正确方法是什么? [英] What's the right way in Java to connect to a Microsoft Access 2007 database?

查看:180
本文介绍了Java中连接到Microsoft Access 2007数据库的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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'.

任何想法?

更新24 / 2009年3月:现在它正在发挥作用。创建了一个用户数据源,由于某种原因,异常消失了。

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

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

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

推荐答案

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

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驱动程序对此更有效,因为它包含在应用程序中,因此一旦安装了应用程序,它就可以连接到数据源了。

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.

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

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

这篇关于Java中连接到Microsoft Access 2007数据库的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆