gpt4 book ai didi

java - 引用其自己的列别名的查询出现 UCanAccess "object not found"错误

转载 作者:行者123 更新时间:2023-11-30 08:00:01 29 4
gpt4 key购买 nike

由于 JAVA 8 不再支持 JDBC:ODBC 桥,我尝试将其替换为 Ucanaccess V3.0,但我遇到了一些问题,请参阅以下内容。

这是我用来连接到我的数据库的代码:

String url = "jdbc:ucanaccess://D:/ADEL/ADEL Local/ADEL_DATA.accdb";
try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

}
catch (java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
ErrorFile.writeError(Thread.currentThread().getStackTrace()[2].getLineNumber(), e);
}

要运行查询,我使用之前在 JDBC:ODBC 中使用的相同代码

Connection c = ConnectDB.doConnect();

String selectString = "SELECT DISTINCT [Maintenance input Check Due List].[Interval (Mos)], [Maintenance input Check Due List].InputAircraftHours, [Maintenance input Check Due List].InputAircraftLandings, [Maintenance input Check Due List].Check, [Maintenance input Check Due List].Title, [Maintenance input Check Due List].InputNumber, [Maintenance input Check Due List].AircraftCurrentTSN, [Maintenance input Check Due List].AircraftCurrentCSN, [Maintenance input Check Due List].Aircraft, [Maintenance input Check Due List].InputDateOUT, [Maintenance input Check Due List].[Interval (Hrs)], [Maintenance input Check Due List].[Interval(Ldgs)], [Maintenance input Check Due List].[NextDueCheckMonth], [Maintenance input Check Due List].NextDueCheckHours, [Maintenance input Check Due List].NextDueCheckLdgs, [Maintenance input Check Due List].RemainCheckMonth, [Maintenance input Check Due List].RemainCheckHours,[Maintenance input Check Due List].RemainCheckLdgs,[Maintenance input Check Due List].InputRepairStation,[Maintenance input Check Due List].InputDescription "
+ "FROM [Maintenance input table] RIGHT JOIN ([Maintenance Schedule Check] RIGHT JOIN [...]

try
{
stmt = c.createStatement();
ResultSet data = stmt.executeQuery(selectString);
System.out.print("(AircraftDueList-ln539) " + "requete passed\n");
while (data.next())
{[...]}
}
catch (SQLException e)
{
ErrorFile.writeError(Thread.currentThread().getStackTrace()[2].getLineNumber(), e);
System.err.println("SQLException: "
+ e.getMessage());
e.printStackTrace();
}

但是这个查询抛出异常:

 SQLException: UCAExc:::3.0.0 user lacks privilege or object not found: NEXTDUECHECKMONTH
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 user lacks privilege or object not found: NEXTDUECHECKMONTH
at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:211)
at main.AircraftDueList.inspPack(AircraftDueList.java:626)
at main.AircraftDueList.createPDF(AircraftDueList.java:88)
at main.AircraftList.<init>(AircraftList.java:166)
at Launch.main(Launch.java:13)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: NEXTDUECHECKMONTH
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)
... 4 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: NEXTDUECHECKMONTH
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ExpressionColumn.checkColumnsResolved(Unknown Source)
at org.hsqldb.RangeVariable.resolveRangeTable(Unknown Source)
at org.hsqldb.QuerySpecification.resolveRangeVariables(Unknown Source)
at org.hsqldb.QuerySpecification.resolveReferences(Unknown Source)
at org.hsqldb.ParserDQL.XreadJoinedTableAsSubqueryOrNull(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)
... 7 more

此查询调用 Access 中的表和存储查询,“NEXTDUECHECKMONTH”是 Access 中名为“[维护输入检查到期列表]”的存储查询的别名字段。

在存储的查询 [维护输入检查到期列表] 中,字段 NEXTDUECHECKMONTH 的构造如下:

IIf([Interval (Mos)] Is Null,Null,DateAdd("m",[Interval (Mos)],[InputDateOUT])) AS NextDueCheckMonth

有人成功解决过此类问题吗?

PS:我的所有查询都在 ACCESS 中成功运行

最佳答案

正如对另一个答案的评论中提到的,问题是查询有一个 WHERE 子句试图引用在同一查询中定义的列别名。这在 UCanAccess 中不起作用,例如

sql = 
"SELECT Date1 AS DateAlias " +
"FROM DateTest " +
"WHERE DateAlias IS NOT NULL";

抛出

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 user lacks privilege or object not found: DATEALIAS

但它在 Access 本身中也不起作用,因为 Access 无法将别名识别为列名,因此会提示我们输入参数值:

ParameterPrompt.png

此问题的一个解决方法是将原始查询封装为子查询,以便外部查询可以使用别名:

sql = 
"SELECT DateAlias " +
"FROM (SELECT Date1 AS DateAlias FROM DateTest) " +
"WHERE DateAlias IS NOT NULL";

关于java - 引用其自己的列别名的查询出现 UCanAccess "object not found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096978/

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