3

每当我尝试使用 ODBC 驱动程序访问 MDB 文件时,它都会给我一个错误:

error : "[Microsoft][ODBC Driver Manager] Invalid string or buffer length exception"

所以我决定改用UCanAccess JDBC 驱动程序

有没有人有使用 Spring 配置 UCanAccess JDBC 驱动程序的经验?

我已将 UCanAccess.jar 放入我的 lib 文件夹并如下配置,但这不起作用:

<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" />

我的开发环境:带有 JdbcTemplate 的 Spring Framework 3.x,Windows 7 64bit,Microsoft Access 2013。

4

2 回答 2

2

您必须将所有 UCanAccess 依赖项放在您的 lib 文件夹中(请参阅 UCanAccess 发行版的 lib 文件夹中的 jar:jackcess、hsqldb、commons-logging 和 commons-lang)。

于 2013-12-27T05:41:17.430 回答
0

这对我来说很好用这种依赖

    <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();
        }
    }
}
}
于 2020-09-27T15:23:26.320 回答