java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver 发生异常。为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22984438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
提问by hina abbasi
I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.
我创建了一个 MS Access 数据库并为其分配了一个 DSN。我想通过我的 Java 应用程序访问它。
This is what I am doing:
这就是我正在做的:
public class AccessDbConnection {
public static void main(String[] args) {
System.out.println("**ACCESS DB CONNECTION**");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // for MS Access ... MS access driver loading
String conURL = "jdbc:odbc:sampleDNS";
Connection con = DriverManager.getConnection(conURL);
Statement statement = con.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = statement.executeQuery(qry);
while(rs.next()) {
String id = rs.getString("ID") ;
String fname = rs.getString("First_Name");
String lname = rs.getString("Last_Name");
System.out.println(id + fname + lname);
}
} catch (ClassNotFoundException ex) {
System.out.println("Classforname Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
System.out.println("DriverManager Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I am getting the exception at the first line of try block. That is class.forname("..");
. Why am I having this Exception?
我在 try 块的第一行收到异常。那就是class.forname("..");
。为什么我有这个异常?
回答by Gord Thompson
For Java 7 you can simply omit the Class.forName()
statement as it is not really required.
对于 Java 7,您可以简单地省略该Class.forName()
语句,因为它并不是真正必需的。
For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccessinstead. For more information, see
对于 Java 8,您不能使用 JDBC-ODBC 桥,因为它已被删除。您将需要使用类似UCanAccess 之类的东西。有关更多信息,请参阅
回答by Umair Bhatti
in JDK 8, jdbc odbc bridge is no longer used and thus removed fro the JDK. to use Microsoft Access database in JAVA, you need 5 extra JAR libraries.
在 JDK 8 中,不再使用 jdbc odbc 桥接器,因此从 JDK 中删除。要在 JAVA 中使用 Microsoft Access 数据库,您需要 5 个额外的 JAR 库。
1- hsqldb.jar
1- hsqldb.jar
2- Hymancess 2.0.4.jar
2- Hymancess 2.0.4.jar
3- commons-lang-2.6.jar
3- commons-lang-2.6.jar
4- commons-logging-1.1.1.jar
4- commons-logging-1.1.1.jar
5- ucanaccess-2.0.8.jar
5- ucanaccess-2.0.8.jar
add these libraries to your java project and start with following lines.
将这些库添加到您的 java 项目并从以下几行开始。
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<Path to your database i.e. MS Access DB>");
Statement s = conn.createStatement();
path could be like E:/Project/JAVA/DBApp
路径可能类似于 E:/Project/JAVA/DBApp
and then your query to be executed. Like
然后你的查询被执行。喜欢
ResultSet rs = s.executeQuery("SELECT * FROM Course");
while(rs.next())
System.out.println(rs.getString("Title") + " " + rs.getString("Code") + " " + rs.getString("Credits"));
certain imports to be used. try catch block must be used and some necessary things no to be forgotten.
要使用的某些进口。必须使用 try catch 块,并且不能忘记一些必要的事情。
Remember, no need of bridging drivers like jdbc odbc or any stuff.
请记住,不需要桥接驱动程序,例如 jdbc odbc 或任何东西。
回答by Hasith Sithila
Setup:
设置:
My OS windows 8 64bit
Eclipse version Standard/SDK Kepler Service Release 2
My JDK is jdk-8u5-windows-i586
My JRE is jre-8u5-windows-i586
This how I overcome my error.
这就是我克服错误的方式。
At the very first my Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
also didn't work.
Then I login to this websiteand downloaded the UCanAccess 2.0.8 zip (as Mr.Gord Thompson said) file and unzip it.
一开始我的Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
也没有用。然后我登录到该网站并下载了 UCanAccess 2.0.8 zip(如 Gord Thompson 先生所说)文件并解压。
Then you will also able to find these *.jar files in that unzip folder:
然后您还可以在该解压缩文件夹中找到这些 *.jar 文件:
ucanaccess-2.0.8.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
Hymancess-2.0.4.jar
Then what I did was I copied all these 5 files and paste them in these 2 locations:
然后我所做的是复制所有这 5 个文件并将它们粘贴到这两个位置:
C:\Program Files (x86)\eclipse\lib
C:\Program Files (x86)\eclipse\lib\ext
(I did that funny thing becoz I was unable to import these libraries to my project)
(我做了那件有趣的事,因为我无法将这些库导入到我的项目中)
Then I reopen the eclipse with my project.then I see all that *.jar files in my project's JRE System Library folder.
然后我用我的项目重新打开 Eclipse。然后我在我的项目的 JRE 系统库文件夹中看到所有 *.jar 文件。
Finally my code works.
最后我的代码有效。
public static void main(String[] args)
{
try
{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\Users\Hasith\Documents\JavaDatabase1.mdb");
Statement stment = conn.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = stment.executeQuery(qry);
while(rs.next())
{
String id = rs.getString("ID") ;
String fname = rs.getString("Nama");
System.out.println(id + fname);
}
}
catch(Exception err)
{
System.out.println(err);
}
//System.out.println("Hasith Sithila");
}
回答by selins sofa
add these dependecies to your .pom file:
将这些依赖项添加到您的 .pom 文件中:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.healthmarketscience.Hymancess</groupId>
<artifactId>Hymancess-encrypt</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
and add to your code to call a driver:
并添加到您的代码中以调用驱动程序:
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://{file_location}/{accessdb_file_name.mdb};memory=false");