3

I'm working in a project in Java in Eclipse EE IDE where I have to query a .accdb file. The problem is when I try to load the driver and then connect to the database it gives me an exception error.

My code:

try{
        String filePath = "//myfilepathtomydb/BLABLA/example.accdb"

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

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

        Connection database = DriverManager.getConnection(url);

        System.out.println("Connection sucessful");

    } catch (ClassNotFoundException e){     
        System.err.println("Got an exception");
        System.err.println(e.getMessage());
        e.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
        // TODO: handle exception
    }

The exception:

Got an exception
sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at project.Main.main(Main.java:15)

I'm using 32-bit Eclipse in a 64-bit Windows and from what i've read this way of connecting to the database is not supported by a 64-bit JRE, so I'm using a chosen 32-bit JRE (jdk1.8.0_05) and in my run configurations I used the '-d32' argument in VM.

Apparently the JdbcOdbcDriver should be inside the rt.jar, but when i look for it i can't find the following package: sun.jdbc.odbc.JdbcOdbcDriver.

Would be appreciated if someone could shed light to my problem, any mistakes or stupid things i said fell free to correct me also.

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
Vitz
  • 101
  • 1
  • 12

4 Answers4

8

According to this post the JDBC-ODBC Bridge was removed in Java8. You can use a JDBC Driver specifically for Access. I see a lot of people mentioning UCanAccess to connect to Access although I have not used it myself.

user432
  • 3,396
  • 12
  • 22
  • Thanks for the answer. So I need to get a 3rd party jdbc-odbc driver basically. By the way, do you think using a earlier Java version where i can use the built-in driver is worth it or will i lose key/special features from this new versions? At the end my project needs to query a .accdb file and return the results through a REST API (probably to be developed in Jersey). – Vitz May 02 '14 at 13:07
  • @Vitz As mentioned in the Oracle blog post the JDBC-ODBC Bridge provided from Oracle was never a real solution and that is why it is removed. I would not develop something where you already know it is not going to work in a future Java release because you will end up having to change it anyways. I'd probably end up using UCanAccess or some other 3rd-party library. – user432 May 02 '14 at 13:10
  • Thanks for the prompt answer mate! Appreciate your help. – Vitz May 02 '14 at 13:49
  • 1
    ( cc: @Vitz ) +1 for UCanAccess. More information can be found [here](http://stackoverflow.com/q/21955256/2144390). – Gord Thompson May 02 '14 at 19:28
4

According to Oracle the JDBC-ODBC Bridge will no longer be included with JDK as of Java SE 8 and that

The ideal is "Pure Java": no native code, no platform dependent features.

that means looking for at least a JDBC type 3 or 4.

Lance Andersen wrote:

The JDBC-ODBC Bridge has always been considered transitional and a non-supported product that was only provided with select JDK bundles and not included with the JRE.

The JDBC-ODBC bridge provides limited support for JDBC 2.0 and does not support more recent versions of the JDBC specification.

You should either use any of third-part database drivers (as Microsoft doesn't provide for any) or use a previous version of java. Anyhow I suggest using a specific Driver, instead of the JDBC-ODBC one.

For that you could look at any of the following:

David
  • 2,755
  • 1
  • 25
  • 32
  • If you will insist on quoting verbatim from third party sources, at least use the blockquote formatting to make it clear which bits of the text are yours and which are lifted from elsewhere. – Ian Roberts May 02 '14 at 13:32
1

Oracle's (and Sun's) official position has long (since JDBC 1.0!) been that --

the [JVM-bundled] JDBC-ODBC Bridge should be considered a transitional solution [...] Oracle [was Sun] does not support the JDBC-ODBC Bridge.

However, my employer, OpenLink Software, has produced enterprise-grade commercial Type 1 Bridges between JDBC and ODBC since JVM 1.0, and current versions are fully compatible with the current JVM 1.8. You can learn more here --

TallTed
  • 8,292
  • 2
  • 18
  • 33
0

ODBC Bridge might not exist in JRE rather than SUN's. Check your JRE.

javadev
  • 1,564
  • 2
  • 15
  • 35