How to Open a JDBC Connection to Microsoft Access

In order to connect to an Access database, you must first install the Microsoft Access Driver. If you're on Windows, you can do this by either installing Access or installing the Access Database Engine. Whichever product you install, you must make sure that the architecture matches your JVM. For example, if you're running Java x64, you'll need an x64 version of the Access Driver.

Once you have installed the Microsoft Access Driver, you should be able to to open a JDBC Connection to a Microsoft Access database using one of the following connection strings:

Access Driver for 2007, 2003, and Below

If you have installed an older version of Access or the Access Database Engine, you can connect to Access using the following url.

     String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + filepath;

Note that the driver bundled with these products are 32-bit and are incompatible with 64-bit Java applications. As a workaround, you will need to install an x64 version Access 2010 or Access Database Engine 2010 (or higher).

Access Driver for 2010 and Above

If you have installed Access 2010 or the Access Database Engine 2010, you can connect to Access using the following url.

     String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + filepath;

Example

Here's an example of how to open a read-only connection to a x32 Microsoft Access database using an x64 Java JVM. For this example to work, I download and installed the Microsoft Access Database Engine 2010 Redistributable. The x64 installer is called "AccessDatabaseEngine_x64.exe".



    java.sql.Connection conn = java.sql.DriverManager.getConnection(
        "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + 
        "DBQ=C:\\temp\\access.mdb;DriverID=22;READONLY=true", "", ""
    );
    

Word of Caution

Note that JDBC connections to Access databases tend to be a bit querky. When iterating through a large number of records, I found that the connection would often "disappear". To circumvent this behaviour, I had to manually reset the database connection periodically (e.g. after a couple hundred records).