1

I'm trying to create a simple connection using the jdbc-odbc bridge:

public static Connection  getConnection() {
    Connection con =null;
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
            "c:\\myfolder\\accesdbfile.accdb";
        con = DriverManager.getConnection(conStr);
    } catch(Exception e) {
        e.printStackTrace();}
    return con;
}

But then I get this exception:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0xa4 Thread 0xec0 DBC 0x2f8574c                                                              Jet'.

Any ideas?

Update 24/Mar/2009: Now it's working. Created a User Data Source, and for some reason the exception went away.

As a general question, What would be the best way to handle database connections in Java?

Leonardo
  • 2,439
  • 6
  • 26
  • 27
  • possible duplicate of ["General error Unable to open registry key Temporary (volatile) ..." from Access ODBC](http://stackoverflow.com/questions/26244425/general-error-unable-to-open-registry-key-temporary-volatile-from-access) – Gord Thompson Oct 13 '14 at 13:50
  • @GordThompson So a question created 5 years ago is a possible duplicate of a question asked 12 days ago? – Leonardo Oct 20 '14 at 13:28
  • 1
    *"If the new question is a better question or has better answers, then vote to close the old one as a duplicate of the new one."* (ref: [here](http://meta.stackexchange.com/a/147651/238021)) – Gord Thompson Oct 20 '14 at 14:06

3 Answers3

6

In general, the best way to work with an RDBMS in Java is by using a JDBC driver that is designed to connect directly to the database. Using the JDBC-ODBC bridge tends to be sloww.

If you are trying to do basic read/write manipulations with an Access database, I would also recommend taking a look at the Jackcess library.

Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
2

To answer your general question I would say the best way to handle database connections in Java is to avoid the JDBC-ODBC bridge. Its OK for testing or learning about JDBC but not for real production use. Also, if you have a data source that does not have its own JDBC driver but it has an ODBC driver then you may not have a choice.

The main reason why I suggest that you stay away from it though is that it makes it difficult to deploy your application. You have to set up the Data Source on the machine that you are running your application from. If you have access to the machine no problem, but suppose you are sending the application off to the client? The pure Java JDBC driver works better for this because it is included as part of your application so once your application is installed it is ready to connect to the data source.

Of course depending on your requirements there are two other types of drivers but thats another discussion.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • Also, Oracle has stated that the JDBC-ODBC Bridge "will be removed in JDK 8" (ref: [here](http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html)). – Gord Thompson Nov 27 '13 at 20:14
0
  1. Go to control panel -- > Administrative tool --> ODBC Data Source Administrator
  2. Add database --> Select "Microsoft Driver(*.mdb, *.accdb)"
  3. Dobule click on new database --> Under "Database" click on "select" --> Select your *.accdb file which you hv created as MS access database.
  4. Say OK and go to your java code
  5. Use: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:filename");

It will surely resolve all your problem.