XuryaX
XuryaX

Reputation: 41

Accessing MS Access Database using Java

            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String database = 
          "jdbc:odbc:Driver={MS Access Database (*.accdb)};DBQ=obn.accdb;";
        c= DriverManager.getConnection(database, "", "");
        s=c.createStatement();
                    string = "IN TRY";
                    s.close();  // Close the statement
                    c.close(); // Close the database. Its no more required
                    JOptionPane.showMessageDialog( null, string );

            }
            catch(Exception e)
            {
                string = "IN exception";
                JOptionPane.showMessageDialog( null, string );
            }

I have tried accessing a MS Access Database using Java using the above code always have an Exception. I have tried a few things

            c= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=obn.mdb");
            s=c.createStatement();

In the first one and the second i get exception -

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified. 

...

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        c= DriverManager.getConnection("jdbc:odbc:obn");
        s=c.createStatement();

Tried doing this like this, adding OBN in ODBC Datasources(32-bit) in windows , selecting the path to the database. But it didnt work either.

It gave the error

The specified DSN contains an architecture mismatch between the Driver and Application

I Normally dont use MS Access but i have to for a University Project

Upvotes: 0

Views: 1860

Answers (2)

XuryaX
XuryaX

Reputation: 41

I found a solution.

connecting using this is correct. c= DriverManager.getConnection("jdbc:odbc:obn"); The database has to be added under odbc in windows administrative tools. Since there is no 64 bit driver, 32 bit driver has to be used. Since 32 bit odbc driver is being used we cant use 64 bit JVM, so changing the JVM from 64 bit to 32 bit from the project manager does the trick , and now it works fine

Upvotes: 0

Gord Thompson
Gord Thompson

Reputation: 123829

Verify that you are using the correct ODBC driver name for your environment. You need to specify either...

{Microsoft Access Driver (*.mdb)}

...or...

{Microsoft Access Driver (*.mdb, *.accdb)}

...depending on whether you want to use the older "Jet" driver (only for .mdb files, available to 32-bit applications only) or the newer "ACE" driver (.mdb or .accdb files, can be used by 32- or 64-bit applications, may need to be installed separately if the machine does not already have Access on it).

Upvotes: 1

Related Questions