Whenever I tried to use an ODBC driver to access MDB files, it gave me an error:
error : "[Microsoft][ODBC Driver Manager] Invalid string or buffer length exception"
So I decided to use the UCanAccess JDBC driver instead.
Does anyone have experience configuring UCanAccess JDBC driver with Spring?
I've put the UCanAccess.jar into my lib folder and configured it like below, but that doesn't work:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" ref="jdbcDriver" />
<property name="url" value="jdbc:ucanaccess://C:\\XXXX.mdb" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="jdbcDriver" class="net.ucanaccess.jdbc.UcanaccessDriver" />
My development environment: Spring Framework 3.x with JdbcTemplate, Windows 7 64bit, Microsoft Access 2013.
您必须将所有 UCanAccess 依赖项放在 lib 文件夹中(请参阅 UCanAccess 发行版的 lib 文件夹中的 jars:jackcess、hsqldb、commons-logging 和 commons-lang)。
This works fine for me with this dependency
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>4.0.0</version>
</dependency>
MsAccessDatabaseConnection.java
package com.test.learn.java8;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MsAccessDatabaseConnection {
public static void main(String[] args) {
// variables
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
// Step 1: Loading driver
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} catch (ClassNotFoundException cnfex) {
System.out.println("Problem in loading or "
+ "registering MS Access JDBC driver");
cnfex.printStackTrace();
}
// Step 2: open db connection
try {
String msAccDB = "C:/Users/vikal/OneDrive/Documents/VikDB.accdb";
//"D:/WORKSPACE/TEST_WORKSPACE/Java-JDBC/Player.accdb";
String dbURL = "jdbc:ucanaccess://" + msAccDB;
// Step 2.A: Create and get connection using DriverManager class
connection = DriverManager.getConnection(dbURL);
// Step 2.B: Creating JDBC Statement
statement = connection.createStatement();
// Step 2.C: Executing SQL & retrieve data into ResultSet
resultSet = statement.executeQuery("SELECT * FROM Employee");
System.out.println("ID\tName\t\t\tAge\tsalary");
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + "\t" +
resultSet.getString(2) + "\t" +
resultSet.getString(3) + "\t" +
resultSet.getString(4));
}
} catch (SQLException sqlex) {
sqlex.printStackTrace();
} finally {
try {
if (null != connection) {
// cleanup resources, once after processing
resultSet.close();
statement.close();
// and then finally close connection
connection.close();
}
} catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.