"UCAExc:::4.0.2 unknown token:" 用于带有日期和时间文字的 INSERT
Posted
技术标签:
【中文标题】"UCAExc:::4.0.2 unknown token:" 用于带有日期和时间文字的 INSERT【英文标题】:"UCAExc:::4.0.2 unknown token:" for INSERT with date and time literals 【发布时间】:2017-09-20 14:07:43 【问题描述】:我有一个正在尝试更新的应用程序,因为它使用的是 jdbc 驱动程序 (sun.jdbc.odbc.jdbcodbcdriver),现在正在使用 Ucanaccess。目前应用程序正确访问数据库,但未能对 Access 数据库进行更改,我认为这可能是由于日期格式或时间格式。
相关,当日期格式从 #01/02/08# 更改为 #01/02/2008# 时,从表中删除行的函数运行成功。
这是查询字符串的输出:
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)
这里是函数的sn-p:
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);
和堆栈跟踪:
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
【问题讨论】:
您不应该将值连接到这样的查询字符串中,而是使用带有参数的准备好的语句。您的代码容易受到 SQL 注入的攻击。 相关:***.com/q/21955256/1531971 & ***.com/q/28439400/1531971 UCanAccess SQL Insert failing; query works fine in MS Access unexpected token (UcanaccessStatement.java:222)的可能重复 感谢 cmets。我可以考虑遵循准备好的陈述。我觉得错误在于时间格式,但我无法找到答案。我可以使用以类似格式编写的函数删除一行,但如果日期在 #01/01/12# 与 #01/01/2012# 中,则会失败。我也研究了上面的相关问题。 【参考方案1】:UCanAccess(当前)不识别仅包含小时和分钟的纯时间文字。但是,它确实可以识别包含小时、分钟和秒的文字。
所以这将失败......
UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#11:11#);
UCanAccess>UCAExc:::4.0.2 unknown token:
...但这有效:
UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#11:11:00#);
UCanAccess>1 row(s) affected
同样,它不识别具有两位数年份的日期文字,因此失败...
UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#12/25/17#);
UCanAccess>UCAExc:::4.0.2 unknown token:
...但这成功了:
UCanAccess>INSERT INTO Table4 (dtmField) VALUES (#12/25/2017#);
UCanAccess>1 row(s) affected
【讨论】:
谢谢,我相信这会回答我的问题!以上是关于"UCAExc:::4.0.2 unknown token:" 用于带有日期和时间文字的 INSERT的主要内容,如果未能解决你的问题,请参考以下文章
UCanAccess ConstraintViolationException 未被捕获