gpt4 book ai didi

java - UCanAccess 无法读取带有 "... FROM TableName IN ' FileSpec 的查询'"

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:09 26 4
gpt4 key购买 nike

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

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

enter image description here

  • DBb(包含 QueryLinkTable {TableOfDBa 的 View })。
  • enter image description here

    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 中创建一个名为 [TableOfDBa] 的实际链接表,该表指向 DBa.mdb 中的表,然后将 DBb.mdb 中的 View (已保存的查询)更改为

    SELECT * FROM TableOfDBa

    Untitled.png

    关于java - UCanAccess 无法读取带有 "... FROM TableName IN ' FileSpec 的查询'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851510/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com