[英]ClassNotFoundException when trying to connect to .accdb file via JDBC-ODBC in Java 8
I'm working in a project in Java in Eclipse EE IDE where I have to query a .accdb
file. 我正在Eclipse EE IDE的Java中的一个项目中工作,我必须在其中查询.accdb
文件。 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. 我在64位Windows中使用32位Eclipse,从我所读到的这种连接数据库的方式中,64位JRE不支持这种方式,因此我使用的是选择的32位JRE( jdk1.8.0_05),在我的运行配置中,我在VM中使用了“ -d32”参数。
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
. 显然, JdbcOdbcDriver
应该位于rt.jar内 ,但是当我寻找它时,找不到以下软件包: 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. 如果有人可以阐明我的问题,我说的任何错误或愚蠢的事情也可以纠正我,我将不胜感激。
According to this post the JDBC-ODBC Bridge was removed in Java8. 根据这篇文章,JDBC-ODBC Bridge已在Java8中删除。 You can use a JDBC Driver specifically for Access. 您可以使用专门用于Access的JDBC驱动程序。 I see a lot of people mentioning UCanAccess to connect to Access although I have not used it myself. 我看到很多人都提到UCanAccess连接到Access,尽管我自己并未使用它。
According to Oracle the JDBC-ODBC Bridge will no longer be included with JDK as of Java SE 8 and that 根据Oracle的说法,自Java SE 8起,JDK将不再包含JDBC-ODBC桥,并且
The ideal is "Pure Java": no native code, no platform dependent features. 理想的是“纯Java”:没有本地代码,没有依赖平台的功能。
that means looking for at least a JDBC type 3 or 4. 这意味着至少要查找JDBC类型3或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. JDBC-ODBC桥一直被认为是过渡性的,并且是不支持的产品,仅随特定的JDK捆绑包提供,而JRE不随附。
The JDBC-ODBC bridge provides limited support for JDBC 2.0 and does not support more recent versions of the JDBC specification. JDBC-ODBC桥对JDBC 2.0提供了有限的支持,并且不支持JDBC规范的最新版本。
You should either use any of third-part database drivers (as Microsoft doesn't provide for any) or use a previous version of java. 您应该使用任何第三方数据库驱动程序(因为Microsoft不提供任何驱动程序)或使用Java的早期版本。 Anyhow I suggest using a specific Driver, instead of the JDBC-ODBC one. 无论如何,我建议使用特定的驱动程序,而不要使用JDBC-ODBC。
For that you could look at any of the following: 为此,您可以查看以下任何一项:
Oracle's (and Sun's) official position has long (since JDBC 1.0!) been that -- 从JDBC 1.0开始,甲骨文(和Sun)的官方地位就一直存在-
the [JVM-bundled] JDBC-ODBC Bridge should be considered a transitional solution [...] Oracle [was Sun] does not support the JDBC-ODBC Bridge. 应当将[JVM捆绑] JDBC-ODBC桥视为过渡解决方案。Oracle [是Sun]不支持JDBC-ODBC桥。
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. 但是,我的雇主OpenLink Software从JVM 1.0开始就在JDBC和ODBC之间产生了企业级商业类型1桥,并且当前版本与当前JVM 1.8完全兼容。 You can learn more here -- 您可以在这里了解更多信息-
ODBC Bridge might not exist in JRE rather than SUN's. ODBC Bridge可能不存在于JRE中,而不存在于SUN中。 Check your JRE. 检查您的JRE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.