问题
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.
回答1:
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.
回答2:
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:
- EasySoft drivers
- UCanAccess
回答3:
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 --
- Single-Tier JDBC-ODBC Bridge Driver -- a JDBC driver for ODBC data sources
- Single-Tier ODBC-JDBC Bridge Driver -- an ODBC driver for JDBC data sources
回答4:
ODBC Bridge might not exist in JRE rather than SUN's. Check your JRE.
来源:https://stackoverflow.com/questions/23428747/classnotfoundexception-when-trying-to-connect-to-accdb-file-via-jdbc-odbc-in-ja