Java: Connect to MS-Access Database (mdb or mde)

风格不统一 提交于 2020-11-27 04:25:48

问题


I am trying to connect to MS-Access using JDBC:ODBC:

public boolean connectToAccess(String accessFilePath) {
    //Get connection to database
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        myConnection = DriverManager.getConnection("jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
    } catch (Exception ex) {
        System.out.println(ex);
        return false;
    }
    return true;
}

I get the error: "No suitable driver found for jdbc: odbc: driver={Microsoft Access Driver (*.mdb)};DBQ=file.mdb" Why? Can you suggest another way of reading access files in Java?


回答1:


Take those spaces out of the connection string and see if that helps. I'd also recommend printing the stack trace.

public boolean connectToAccess(String accessFilePath) {
    //Get connection to database
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        myConnection = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessFilePath);
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
    return true;
}



回答2:


The other way to read Access files is using the Jackcess library.




回答3:


Try to create a DSN for the Access database from odbcad32. Another issue may be, the driver is not installed on your machine or you have insufficient privileges.



来源:https://stackoverflow.com/questions/6251308/java-connect-to-ms-access-database-mdb-or-mde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!