开发者

How to connect to MS Access DB using Java + ODBC on Windows 7

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(md开发者_运维问答b) 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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜