插入到Ucanaccess中给出错误 [英] INSERT INTO giving error in Ucanaccess

查看:150
本文介绍了插入到Ucanaccess中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert a row into a table (userGames), using Ucanaccess, and, when executing the statement, am recieving the following error:

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

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

In my connect class, i have the following:

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语句:

I am sending the following SQL statement to the update method:

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

我不知道什么是未知令牌",或者我应该怎么做才能纠正错误.

I have no idea what an "unknown token" is, or what I should do to rectify the error.

我已经阅读了有关Ucanaccess的指南,据我所知,它们的作用与我所做的相同. .execute和.executeUpdate都抛出此错误,我不知道该怎么办.

I have read the guide for Ucanaccess, and they do the same thing as I am doing, as far as I can see. Both .execute and .executeUpdate throw this error, and I do not know what to do.

感谢您的帮助

Riccorbypro

Riccorbypro

推荐答案

之所以发生此错误,是因为您已将字符串粘贴到SQL中,而没有将它们括在' '标记中,因此SQL中包含一些单词,表示数据库不知道该怎么解释.

The error is happening because you have pasted strings into your SQL, without enclosing them in ' ' marks, so the SQL has some words in it that the database doesn't know how to interpret.

您应该使用PreparedStatement,并将users[id]games[i]设置为PreparedStatement中的参数. Java教程

You should be using a PreparedStatement, and setting users[id] and games[i] to parameters within the PreparedStatement. This is explained fully in The Java Tutorials

这篇关于插入到Ucanaccess中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆