当尝试通过Java 8中的JDBC-ODBC连接到.accdb文件时,ClassNotFoundException [英] ClassNotFoundException when trying to connect to .accdb file via JDBC-ODBC in Java 8
问题描述
.accdb
文件。 问题是当我尝试加载驱动程序,然后连接到数据库它给我一个异常错误。
我的代码:
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;
连接数据库= 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
}
异常:
有一个异常
sun.jdbc.odbc.JdbcOdbcDriver
java.lang.ClassNotFoundException:sun.jdbc.odbc .JdbcOdbcDriver
在java.net.URLClassLoader $ 1.run(URLClassLoader.java:372)
在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361)
在java.security .AccessController.doPrivileged(Native Method)
在java.net.URLClassLoader.findClass(URLClassLoader.java:360)
在java.lang.ClassLoader.loadClass(ClassLoader.java:424)
在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
在java.lang.Class.forName0(本机方法)
在java.lang.Class.forName(Class.java:259)
在project.Main.main(Main.java:15)
我在64位Windows中使用32位Eclipse,从我以这种方式读取connec 64位JRE不支持数据库,所以我使用一个选择的32位JRE(jdk1.8.0_05),在我的运行配置中,我在VM中使用了'-d32'参数。
显然, JdbcOdbcDriver
应该在 rt.jar 内,但是当我找到它找不到以下软件包: sun.jdbc.odbc.JdbcOdbcDriver
。
如果有人可以解释我的问题,任何错误或愚蠢的事情,我说可以自由地纠正我。
根据此发布JDBC-ODBC Bridge在Java8中被删除。您可以使用专门用于Access的JDBC驱动程序。我看到很多人提到 UCanAccess 连接到Access,尽管我还没有使用它。
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.
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.
这篇关于当尝试通过Java 8中的JDBC-ODBC连接到.accdb文件时,ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!