java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver 发生异常.为什么? [英] java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
问题描述
我创建了一个 MS Access 数据库并为其分配了一个 DSN.我想通过我的 Java 应用程序访问它.
I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.
这就是我正在做的:
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);
}
}
}
我在 try 块的第一行收到异常.那就是class.forname("..");
.为什么我有这个异常?
I am getting the exception at the first line of try block. That is class.forname("..");
. Why am I having this Exception?
推荐答案
对于 Java 7,您可以简单地省略 Class.forName()
语句,因为它并不是真正必需的.
For Java 7 you can simply omit the Class.forName()
statement as it is not really required.
对于 Java 8,您不能使用 JDBC-ODBC 桥,因为它已被删除.您将需要使用诸如 UCanAccess 之类的东西.如需更多信息,请参阅
For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. For more information, see
在没有 ODBC 的情况下从 Java 操作 Access 数据库
这篇关于java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver 发生异常.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!