在 Ucanaccess 中插入错误

Posted

技术标签:

【中文标题】在 Ucanaccess 中插入错误【英文标题】:INSERT INTO giving error in Ucanaccess 【发布时间】:2015-02-10 18:46:49 【问题描述】:

我正在尝试使用Ucanaccess 在表 (userGames) 中插入一行,并且在执行语句时收到以下错误:

Feb 10, 2015 8:29:02 PM db.Connect update
SEVERE: null
net.ucanaccess.jdbc.UcanaccessSQLException: unknown token: 
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:145)
at db.Connect.update(Connect.java:42)
at db.GameScanner.searchGames(GameScanner.java:99)
at db.GameScanner.<init>(GameScanner.java:91)
at db.WelcomeGUI.scanButtonActionPerformed(WelcomeGUI.java:143)
at db.WelcomeGUI.access$000(WelcomeGUI.java:24)
at db.WelcomeGUI$1.actionPerformed(WelcomeGUI.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
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:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
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.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at net.ucanaccess.jdbc.Execute.executeWrapped(Execute.java:62)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:138)
at net.ucanaccess.jdbc.Execute.execute(Execute.java:52)
at net.ucanaccess.jdbc.UcanaccessStatement.execute(UcanaccessStatement.java:143)
... 42 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.readColumnOrFunctionExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadSimpleValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadRowElementList(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionOrNull(Unknown Source)
at org.hsqldb.ParserDQL.XreadValueExpressionWithContext(Unknown Source)
at org.hsqldb.ParserDQL.readRow(Unknown Source)
at org.hsqldb.ParserDQL.XreadContextuallyTypedTable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(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)
... 48 more

在我的连接类中,我有以下内容:

private Connection conn;

public Connect(String filepath) 
    try 
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        conn = DriverManager.getConnection("jdbc:ucanaccess://" + filepath + ";");
        System.out.println("Connection successful");

     catch (Exception e) 
        System.out.println("ERROR: " + e);
        e.printStackTrace();
    

以及更新/插入方法:

public boolean update(String sql) 
    boolean result = false;
    try 
        Statement stmt = conn.createStatement();
        result = stmt.execute(sql);
     catch (SQLException ex) 
        Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
    
    return result;

我正在向更新方法发送以下 SQL 语句:

"INSERT INTO userGames (userID,gameID) VALUES ("+users[id]+","+games[i]+")"

我不知道什么是“未知令牌”,或者我应该如何纠正错误。

我已经阅读了 Ucanaccess 指南,据我所知,他们做的事情和我一样。 .execute 和 .executeUpdate 都抛出这个错误,我不知道该怎么办。

感谢您的帮助

Riccorbypro

【问题讨论】:

【参考方案1】:

发生错误是因为您将字符串粘贴到 SQL 中,而没有将它们括在 ' ' 标记中,因此 SQL 中有一些数据库不知道如何解释的词。

您应该使用PreparedStatement,并将users[id]games[i] 设置为PreparedStatement 中的参数。这在The Java Tutorials中得到了充分的解释

【讨论】:

【参考方案2】:

大卫·华莱士怎么说.....

"INSERT INTO userGames (userID,gameID) VALUES ('"+users[id]+'",'"+games[i]+"')"

最简单的调试方法是在执行之前打印 SQL 语句。

但你真的应该使用PreparedStatement

【讨论】:

无论您做什么,都不要养成以这种方式连接字符串来构建 SQL 的习惯。创建 SQL 注入漏洞只是时间问题。从一开始就学习正确的做法。使用PreparedStatement

以上是关于在 Ucanaccess 中插入错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Ucanaccess 中插入错误

如何使用 UCanAccess 从 Java 将两个字符串插入我的 Access 数据库?

使用 UCanAccess 从大文本文件中插入数据非常慢

我正在尝试将一个简单的查询插入到 Microsoft 数据库中,并且我正在使用 UCanAccess

通过 UCanAccess 插入时出现“强制转换的字符值无效”错误

UCanAccess/Jackcess 在 Access 中创建查看/保存查询