2

I have an application I am trying to update since it was using a jdbc driver (sun.jdbc.odbc.jdbcodbcdriver), and am now using Ucanaccess. Currently the application correctly accesses the database, but is failing on making changes to the Access database, which I believe may be due to the date format or time format.

related, a function to delete a row from a table ran successfully when the date format was changed from #01/02/08# to #01/02/2008#.

Here is an output of the queryString:

Insert into
follow(
f_c_id,
f_date,
f_time_begin,
f_time_end,
f_flag_begin_in,
f_flag_end_in,
start_map_number,
end_map_number,
b_observer,
map_observer,
start_Map_Time,
end_Map_Time,
f_duration) values (
'ert',
#11/11/1989#,
#11:11#,
#12:12#,
-1,
-1,
1,
21,
'sf',
'sdg',
#12:12#,
#13:13#,
61)

Here a snippet of the function:

try {

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Data/Example.mdb", "", "");
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        queryString = "Insert into following (f_c_id, f_date, f_time_begin, f_time_end, " +
                "f_flag_begin_in, f_flag_end_in, start_map_number, end_map_number, b_observer," +
                "map_observer, start_Map_Time, end_Map_Time,f_duration)" +
                " values ('" + IDText.getText() +
                "'," + "#" + focalDate + "#" +
                "," + "#" + focalTimeStart + "#" +
                "," + "#" + focalTimeEnd + "#" +
                "," + startNestCheck + "," + endNestCheck +
                "," + startMapNumText.getText() +
                "," + endMapNumText.getText() +
                ",'" + bObserverText.getText() +
                "','" + mapObserverText.getText() +
                "'," + "#" + startMapTime + "#" +
                "," + "#" + endMapTime + "#" +
                "," + fDuration +
                ")";
        System.out.print(queryString);
        stmt.executeUpdate(queryString);
        conn.close();
    } catch (Exception e) {
        if (!(e.getMessage().equalsIgnoreCase("No ResultSet was produced"))) {
            e.printStackTrace();
            System.exit(1);
        }
    }

and stacktrace:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.2 unknown token: 
at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:221)
at Test.clearTable(Test.java:2335)
at Test.genericButtonActionPerformed(Test.java:2266)
at Test.access$1200(Test.java:20)
at Test$14.actionPerformed(Test.java:801)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.sql.SQLSyntaxErrorException: unknown token: 
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.executeUpdate(Unknown Source)
at net.ucanaccess.jdbc.ExecuteUpdate.executeWrapped(ExecuteUpdate.java:67)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:208)
at net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:50)
at net.ucanaccess.jdbc.UcanaccessStatement.executeUpdate(UcanaccessStatement.java:219)
... 40 more
Caused by: org.hsqldb.HsqlException: unknown token: 
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ParserBase.read(Unknown Source)
at org.hsqldb.ParserDQL.XreadPredicateRightPart(Unknown Source)
at org.hsqldb.ParserDQL.XreadBooleanPrimaryOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadBooleanTestOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadBooleanFactorOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadBooleanTermOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadBooleanValueExpression(Unknown Source)
at org.hsqldb.ParserDML.compileDeleteStatement(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)
... 46 more

Process finished with exit code 1
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
  • 1
    You should not concatenate values into a query string like that, use a prepared statement with parameters instead. Your code is vulnerable to SQL injection. – Mark Rotteveel Sep 20 '17 at 14:41
  • Related: https://stackoverflow.com/q/21955256/1531971 & https://stackoverflow.com/q/28439400/1531971 –  Sep 20 '17 at 15:27
  • Possible duplicate of [UCanAccess SQL Insert failing; query works fine in MS Access unexpected token (UcanaccessStatement.java:222)](https://stackoverflow.com/questions/35492250/ucanaccess-sql-insert-failing-query-works-fine-in-ms-access-unexpected-token-u) – tk421 Sep 20 '17 at 15:59
  • Thank you for the comments. I may consider following prepared statements. I feel that the error lies with the time format, but I was unable to find an answer. I was able to delete a row using a function written in a similar format, but would fail if the date was in #01/01/12# vs #01/01/2012#. I researched the related questions above as well. – Jonathan Valdivieso Sep 20 '17 at 16:36

1 Answers1

2

UCanAccess does not (currently) recognize time-only literals that include just the hour and minute. However, it does recognize literals that include the hour, minute, and second.

So this will fail ...

UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#11:11#);
UCanAccess>UCAExc:::4.0.2 unknown token:

... but this works:

UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#11:11:00#);
UCanAccess>1 row(s) affected

Similarly, it does not recognize date literals with two-digit years, so this fails ...

UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#12/25/17#);
UCanAccess>UCAExc:::4.0.2 unknown token:

... but this succeeds:

UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#12/25/2017#);
UCanAccess>1 row(s) affected
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342