如何在 Windows 7 上使用 Java + ODBC 连接到 MS Access DB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4235504/
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
How to connect to MS Access DB using Java + ODBC on Windows 7
提问by Bored Elf
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():
我在谷歌上搜索了很多这个问题,答案不一致,没有任何帮助。所以我想我可以在这里得到更多帮助。我需要用 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
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.
并且不起作用。我不知道如何解决问题,因为我不知道问题出在哪里!如果是 ODBC 驱动程序或 Java 或其他什么。
It's too bothersome to use a virtual WinXP to do the job. This can't go on forever.
使用虚拟的 WinXP 来完成这项工作太麻烦了。这不能永远持续下去。
[EDIT2]
[编辑2]
Just to be sure: it does work on WinXP, as implicited. :}
可以肯定的是:正如隐含的那样,它确实适用于 WinXP。:}
[/EDIT2]
[/编辑2]
I would appreciate a lot any help I could get here. :DDDDD
我会很感激我能在这里得到的任何帮助。:DDDDD
回答by mezzie
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.
因此,由于您已经安装了 ODBC,您可能还想确保使用相同的 64 位 odbc 驱动程序(如果您在 %WINDIR%\System32\odbcad32.exe 下创建了 odbc 连接)。我知道没有用于访问 2007 的 64 位驱动程序,所以在我的情况下,我必须创建一个 32 位 odbc 连接(%WINDIR%\SysWOW64\odbcad32.exe),将我的 java 版本降级到 32 位并使用32 位驱动程序使其工作。
Hope this helps!
希望这可以帮助!