Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

ms access - java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?

I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Java 7 you can simply omit the Class.forName() statement as it is not really required.

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

Manipulating an Access database from Java without ODBC


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...