如何将 MS Access 数据库与 Java 8 连接?
Posted
技术标签:
【中文标题】如何将 MS Access 数据库与 Java 8 连接?【英文标题】:How to connect MS Access database with Java 8? 【发布时间】:2015-08-02 19:15:49 【问题描述】:我想做的事:
我想用 Java 8 连接我的 MS Access 数据库。所以我使用“UcanAccess”驱动程序连接到我的数据库。
我在用什么:
Eclipse、Java 8 和 MS Access 数据库
我的 MS Access 数据库路径:
C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb"
我的项目中有以下 Jar 文件:
mysql-connector-java-5.1.35-bin.jar
ucanaccess-2.095.jar
jackcess-2.1.2.jar
hsqldb.jar
commons-lang3-3.4.jar
commons-logging-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();
【问题讨论】:
尝试使用commons-lang
jar 的 2.0 版 (commons-lang-2.0.jar)。
我猜,您的 JAR 中有一些 不匹配,请查看下面的文章以获取完整示例,其中包含驱动程序和相关 JAR 信息benchresources.net/…
【参考方案1】:
作为you can see,jackcess 2.1.2 依赖于 commons-lang 2.6。您使用的版本 (3.4) 不向后兼容。
如果您的目标是连接到 MS Access,为什么还要为 MySQL 和 HSQLDB 提供 JDBC 驱动程序?
【讨论】:
很好的链接。旧版本是为了向后兼容。 Access 数据库的UCanAccess JDBC 驱动程序使用 HSQLDB 作为后备数据库(通常仅在内存中)。以上是关于如何将 MS Access 数据库与 Java 8 连接?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 MS Access 实现 Java Persistence Api?