Search code examples
javams-accessucanaccess

UCanAccess "object not found" error for query that references its own column alias


Since JDBC:ODBC bridge in not longer supported by JAVA 8 I try to replace it with Ucanaccess V3.0, but II am facing some issue, see following.

This is the code I use in order to connect to my database :

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);
        }

To run query I the same code I previously using with 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();
    }

But this query throw an exception :

 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

This query call tables and stored query in access, "NEXTDUECHECKMONTH" is an alias field from a stored query in access named "[Maintenance input Check Due List]".

In the stored query [Maintenance input Check Due List] the field NEXTDUECHECKMONTH is construct like this :

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

Someone have successfully facing this kind of issue ?

PS : All my query's run successfully in ACCESS


Solution

  • As mentioned in a comment to the other answer, the problem was that the query had a WHERE clause trying to reference a column alias that was defined in the same query. That doesn't work in UCanAccess, e.g.,

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

    throws

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

    but it also does not work in Access itself since Access does not recognize the alias as a column name and therefore prompts us for a parameter value:

    ParameterPrompt.png

    One workaround for this issue would be to encapsulate the original query as a subquery so the outer query can use the alias:

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