如何使用 Java 连接到 MS Access DB Windows 7 上的 ODBC

发布于 2024-10-03 17:36:56 字数 1103 浏览 0 评论 0原文

我用谷歌搜索了很多这个问题,答案不一致,没有任何帮助。所以我想我可以在这里得到更多帮助。 我需要为我的下一堂课用 Java 编写一个小应用程序,该应用程序使用 ODBC 从 MS Access 数据库(mdb)获取数据。我的操作系统是 Windows 7 Ultimate 64 位。我知道SysWOW64文件夹中有一个odbcad32。 我设法在那里设置了一个 DSN。但我无法连接。应用程序抛出 SQL 异常。尝试了 DriverManager.getConnection() 的两种替代方法:

jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBH=E:/DB.mdb

并且

jdbc:odbc:DSN_NAME

驱动程序是:

sun.jdbc.odbc.JdbcOdbcDriver

[编辑]

连接代码基本上如下:

    try {
        Class.forName(driver);
        db = DriverManager.getConnection(url);
    }
    catch(SQLException e) {
        System.out.println("SQL error!");
    }
    catch(ClassNotFoundException e) {
        System.out.println("Class not found!");
    }

[/编辑]

并且不起作用。我不知道如何解决这个问题,因为我不知道问题出在哪里!如果是 ODBC 驱动程序或 Java 或其他什么。

使用虚拟WinXP来完成这项工作太麻烦了。这不可能永远持续下去。

[EDIT2]

可以肯定的是:正如隐含的那样,它确实可以在 WinXP 上运行。 :}

[/EDIT2]

如果我能在这里得到任何帮助,我将不胜感激。 :DDDDDD

I googleed this question a lot and the answers are inconsistent and none helped. So I thought I could get a little more help here.
I need to code a little app in Java for my next class which gets data from a MS Access database(mdb) using ODBC. My OS is Windows 7 Ultimate 64bits. I know there is a odbcad32 in SysWOW64 folder.
I managed to set up a DSN there. But I can't connect. The app throws a SQL Exception. Tried two alternatives for DriverManager.getConnection():

jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBH=E:/DB.mdb

and

jdbc:odbc:DSN_NAME

The driver is:

sun.jdbc.odbc.JdbcOdbcDriver

[EDIT]

The connection code is basically the following:

    try {
        Class.forName(driver);
        db = DriverManager.getConnection(url);
    }
    catch(SQLException e) {
        System.out.println("SQL error!");
    }
    catch(ClassNotFoundException e) {
        System.out.println("Class not found!");
    }

[/EDIT]

And doesn't work. I don't know how to fix the problem because I don't know where the problem lies! If is the ODBC driver or Java or whatever.

It's too bothersome to use a virtual WinXP to do the job. This can't go on forever.

[EDIT2]

Just to be sure: it does work on WinXP, as implicited. :}

[/EDIT2]

I would appreciate a lot any help I could get here. :DDDDD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-10-10 17:36:56

因此,既然您已经设置了 ODBC,您可能还需要确保使用相同的 64 位 odbc 驱动程序(如果您在 %WINDIR%\System32\odbcad32.exe 下创建了 odbc 连接)。我知道事实上没有用于 access 2007 的 64 位驱动程序,因此在我的情况下,我必须创建一个 32 位 odbc 连接(%WINDIR%\SysWOW64\odbcad32.exe),将我的 java 版本降级到 32 位并使用32 位驱动程序使其工作。

希望这有帮助!

So you since you already have setup ODBC, you might want to also make sure you are using the same 64 bit odbc driver (if you created your odbc connection under %WINDIR%\System32\odbcad32.exe). I know for a fact that there is no 64 bit driver for access 2007 so in my case, I had to create a 32 bit odbc connection(%WINDIR%\SysWOW64\odbcad32.exe), downgrade my java version to 32 bits and use the 32 bit driver to make it to work.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文