如何将MS Access数据库与Java 8连接?

时间:2015-08-02 19:15:49

标签: java ucanaccess

我要做的是:

我想将我的MS Access数据库与Java 8连接。所以我使用“UcanAccess”驱动程序连接到我的数据库。

我正在使用的内容:

Eclipse,Java 8和MS Access数据库

我的MS Access数据库路径

C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb"

我的项目中有以下Jar文件:

  • MySQL的连接器的Java-5.1.35-bin.jar

  • ucanaccess-2.095.jar

  • jackcess-2.1.2.jar

  • 的hsqldb.jar

  • 公地lang3-3.4.jar

  • 共享记录-1.2.jar

错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:106)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:2039)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1847)
at java.util.Collections.indexedBinarySearch(Unknown Source)
at java.util.Collections.binarySearch(Unknown Source)
at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2570)
at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:844)
at com.healthmarketscience.jackcess.impl.IndexData.access$3700(IndexData.java:47)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2335)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2273)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2256)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2218)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:376)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:282)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:153)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:2074)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1953)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:858)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:518)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:389)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:38)
at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:158)
at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:103)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ex01.main(ex01.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 28 more

连接数据库的代码:

public class ex01 {
public static void main(String[] args) {
    String url = "jdbc:ucanaccess://C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb";

    Connection con;

    Statement stmt;

    String query = "Select * from user";

    try {
        // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

        con = DriverManager.getConnection(url, "", "");

        stmt = con.createStatement();

        // Returns a ResultSet that contains the data produced by the query;
        // never null
        ResultSet rs = stmt.executeQuery(query);

        System.out.println("User Data:");
        System.out.println("FirstName\tLastName\tAge");

        while (rs.next()) {
            String fName = rs.getString("FirstName");
            String lName = rs.getString("LastName");
            int age = rs.getInt("age");
            System.out.println(fName + "\t" + lName + "\t" + age);
        }

        stmt.close();

        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:7)

作为you can see,jackcess 2.1.2取决于公共场所2.6。您使用的版本(3.4)不向后兼容。

如果你的目标是连接到MS Access,为什么你有MySQL和HSQLDB的JDBC驱动程序?