引用其自己的列别名的查询的 UCanAccess“找不到对象”错误
Posted
技术标签:
【中文标题】引用其自己的列别名的查询的 UCanAccess“找不到对象”错误【英文标题】:UCanAccess "object not found" error for query that references its own column alias 【发布时间】:2015-08-19 13:31:05 【问题描述】:由于 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
此查询调用表和访问中的存储查询,“NEXTDUECHECKMONTH”是访问中名为“[维护输入检查到期清单]”的存储查询的别名字段。
在存储的查询 [Maintenance input Check Due List] 中,字段 NEXTDUECHECKMONTH 的结构如下:
IIf([Interval (Mos)] Is Null,Null,DateAdd("m",[Interval (Mos)],[InputDateOUT])) AS NextDueCheckMonth
有人成功解决过这类问题吗?
PS : 我的所有查询都在 ACCESS 中成功运行
【问题讨论】:
请edit 向我们展示产生错误的minimal reproducible example 的Java 代码。我无法在保存的查询上使用简单的 SELECT 语句重新创建您的问题。 我试图改进我的帖子,我认为过去的查询不是很有帮助,因为它是一个大而令人困惑的查询。 【参考方案1】:正如对另一个答案的评论中提到的,问题是查询有一个 WHERE 子句试图引用在同一查询中定义的列别名。这在 UCanAccess 中不起作用,例如,
sql =
"SELECT Date1 AS DateAlias " +
"FROM DateTest " +
"WHERE DateAlias IS NOT NULL";
抛出
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 用户缺少权限或找不到对象:DATEALIAS
但它在 Access 本身中也不起作用,因为 Access 不会将别名识别为列名,因此会提示我们输入参数值:
解决此问题的一种方法是将原始查询封装为子查询,以便外部查询可以使用别名:
sql =
"SELECT DateAlias " +
"FROM (SELECT Date1 AS DateAlias FROM DateTest) " +
"WHERE DateAlias IS NOT NULL";
【讨论】:
【参考方案2】:我发现了问题:我尝试在查询中创建别名并在此查询中使用它们; Ucanaccess 不喜欢它。
【讨论】:
您是在谈论查询的 FROM 子句中的表别名,例如,... FROM ... RIGHT JOIN [Maintenance input Check Due List] AS micdl ...
?如果是这样,那么您还需要在列列表 (SELECT ...) 中使用表别名(而不是表/查询名称)。
我说的是一个行别名:在这个查询中我有IIf([Interval (Mos)] Is Null,Null,DateAdd("m",[Interval (Mos)],[InputDateOUT])) AS NextDueCheckMonth
,但是在有像WHERE NextDueCheckMonth = ...
这样的条件之后的一些行来解决这个问题我用WHERE IIf([Interval (Mos)] Is Null,Null,DateAdd("m",[Interval (Mos)],[InputDateOUT])) = ...
替换它以上是关于引用其自己的列别名的查询的 UCanAccess“找不到对象”错误的主要内容,如果未能解决你的问题,请参考以下文章
当别名包含某些字符时,UCanAccess 出现“意外令牌”错误
为啥 hibernate 在不同的 JVM 实例中为相同的查询创建不同的列别名?