INSERT INTO 在 Ucanaccess 中给出错误

INSERT INTO giving error in Ucanaccess

我正在尝试使用 Ucanaccess 向 table (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[=10=]0(WelcomeGUI.java:24)
at db.WelcomeGUI.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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

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

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

和update/insert方法:

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]+")"

我不知道 "unknown token" 是什么,或者我应该如何纠正错误。

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

感谢您的帮助

Riccorbypro

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

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

中有详细解释

大卫·华莱士所说的......

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

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

但你真的应该使用 PreparedStatement