Connect to MS access databases in java

3.6k views Asked by At

I am trying to input some data into a microsoft access database, but I always get the same error. Here is my code:

import java.sql.Connection;
import java.sql.DriverManager;

public class testDDB {

public static void main(String[] args) {
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
       String filename = "C:\\test\\Database1.mdb";
       String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
       database+= filename.trim() + ";}"; // add on to the end 
       Connection con = DriverManager.getConnection( database ,"",""); 
    }catch(Exception e){
        e.printStackTrace();
    }

}

I get the following errors:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

I have looked in previous posts, I found similar issues but I still do not find the solution. My computer is running on 64bit, and when I go to C:\Windows\SysWOW64\obdcad32.exe, I find enter image description here

So it seems that I have a 64bit version of ODBC...

1

There are 1 answers

0
Gord Thompson On BEST ANSWER

when I go to C:\Windows\SysWOW64\obdcad32.exe, I find

| MS Access Database ... Microsoft Access Driver (*.mdb, *.accdb)

So it seems that I have a 64bit version of ODBC

The folder name "SysWOW64" can be a bit misleading. It contains the components for the 32-bit "WOW" subsystem (i.e., "[32-bit]Windows On Windows[64]").

So, "SysWOW64\obdcad32.exe" is actually the 32-bit ODBC administrator and you in fact have the 32-bit version of the Access "ACE" ODBC driver installed. Therefore you need to run your application under a 32-bit version of the JRE (Java Runtime Environment) in order to use that ODBC driver.

Also, bear in mind that the JDBC-ODBC Bridge was removed from Java 8, so your JRE must be for Java 7 or earlier. (For Java 8 and later, consider using the UCanAccess JDBC driver. Details here.)