2015-02-10 163 views
0

我想插入一行到一個表(userGames)給錯誤,使用Ucanaccess,並且,在執行語句時,我recieving以下錯誤:INSERT INTO在Ucanaccess

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

0

大衛·華萊士說什麼充分說明.....

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

調試這是隻打印您的SQL語句在執行之前,最簡單的方法。

但你真的應該使用PreparedStatement

+0

不管你做什麼,不進入以這種方式連接字符串建立SQL的習慣。創建SQL注入漏洞只是時間問題。從一開始就學習正確的做法。使用'PreparedStatement'。 – 2015-02-10 19:04:32