UCanAccess 无法读取带有“... FROM TableName IN 'FileSpec'”的查询

UCanAccess cannot read query with "... FROM TableName IN 'FileSpec'"

我需要使用 UCanAccess 从 MS Access 的视图中读取数据。我在 C:\DataBases

中有两个数据库
  1. DBa(包含 table:TableOfDBa)。

  1. DBb(包含 QueryLinkTable {TableOfDBa 视图})。

QueryLinkTable 是:

SELECT * FROM TableOfDBa IN 'C:\DataBases\DBa.mdb';

密码是:

package es.fips;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ReadData {
    private static String pathDB = "//C:/DataBases/DBb.mdb";

    public static void main(String[] args) {
        Connection conn = getConnectionOn();
        String nameView = "QueryLinKTable";

        if(conn != null){
            System.out.println("Connected Data Base =D");

            if(thereAreViews(conn)){                
                // This can not read the query from view
                printView(conn, nameView);
            }

            printView(conn, nameView);
        }
    }

    public static Connection getConnectionOn() {
        Connection myConnection = null;
        try {
            myConnection = DriverManager.getConnection("jdbc:ucanaccess:" + pathDB);
            return myConnection;

        } catch (SQLException e) {
            System.out.println("Error connecting");
            return null;
        }
    }

    public static boolean thereAreViews(Connection oneConn) {
        try {
            DatabaseMetaData metaDB = oneConn.getMetaData();
            String[] types = { "VIEW" };
            int numViews = 0;

            ResultSet rs = metaDB.getTables(null, null, null, types);
            while (rs.next()) {
                System.out.println("View " + (numViews + 1) + ": " + rs.getString(3));
                numViews++;
            }

            if (numViews > 0) {
                return true;
            } else {
                System.out.println("There are no views");
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void printView(Connection oneConn, String nameView) {
        try {
            Statement st = oneConn.createStatement();
            String query = "SELECT * FROM " + nameView;
            ResultSet rs = st.executeQuery(query);

            while (rs.next()) {
                System.out.println("Read! =D");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

错误是:

Connected Data Base =D
There are no views
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 usuario no tiene privilegios suficientes o objeto no encontrado: QUERYLINKTABLE
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211)
    at es.fips.ReadData.printView(ReadData.java:69)
    at es.fips.ReadData.main(ReadData.java:25)
Caused by: java.sql.SQLSyntaxErrorException: usuario no tiene privilegios suficientes o objeto no encontrado: QUERYLINKTABLE
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:208)
    ... 2 more
Caused by: org.hsqldb.HsqlException: usuario no tiene privilegios suficientes o objeto no encontrado: QUERYLINKTABLE
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source)
    at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 5 more

如果您知道任何解决方案,我将不胜感激

目前(从3.0.6版本开始),UCanAccess不支持语法

SELECT * FROM TableName IN 'C:\Path\To\DbFile.mdb'

如果您尝试使用 UCanAccess 控制台(console.bat 或 console.sh)打开 DBb.mdb,您将看到

Error occured at the first loading attempt of QueryLinKTable
Converted view was :CREATE VIEW QUERYLINKTABLE AS SELECT *
FROM TableOfDBa IN 'C:\Users\Public\test\DBa.mdb'
Error message was :unexpected token: IN : line: 2

在您的情况下,您需要在 DBb.mdb 中创建一个 实际链接的 table,名为 [TableOfDBa],指向 table在 DBa.mdb 中,然后将 DBb.mdb 中的视图(保存的查询)更改为

SELECT * FROM TableOfDBa