2014-04-04 65 views
1

我使用mysql和具有從GUI選擇字段添加到我的查詢。但是,我得到一個mysql例外此查詢:String query = "INSERT INTO racebikes " + "VALUES(?,?,?)"; 爲了測試措施我打印出來的查詢,以確保該領域是正確的,當填充讀作:INSERT INTO racebikes VALUES('BMC','Switzerland',10000.0)。這是如何拋出一個語法異常?JDBC Mysql的語法錯誤

堆棧跟蹤:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?)' at line 1 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828) 
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1842) 
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764) 
at JDBC_GUI$7.actionPerformed(JDBC_GUI.java:375) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
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:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) 
at java.awt.Component.processEvent(Component.java:6270) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
at java.awt.Container.dispatchEventImpl(Container.java:2273) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:694) 
at java.awt.EventQueue$3.run(EventQueue.java:692) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:708) 
at java.awt.EventQueue$4.run(EventQueue.java:706) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

段:

public void actionPerformed(ActionEvent e) { 
    String query = "INSERT INTO racebikes " + "VALUES(?,?,?)"; 

     try{ 
      statement = connection.prepareStatement(query); 
      statement.setString(1, (String)bikes_combo_box.getSelectedItem()); 
      statement.setString(2, bike_fields[0].getText()); 
      statement.setDouble(3, Double.parseDouble(bike_fields[1].getText())); 

      statement.executeUpdate(query); 

     } catch(SQLException e1) { 
       e1.printStackTrace(); 
     } 
+0

你能JDBC_GUI.java:375在語句準備和變量綁定之前添加的代碼片斷? – Glenn

+0

肯定@Glenn它的! – theGrayFox

+0

它看起來不錯,我(比資源管理等)。我會想象如果'racebikes'和'VALUES'之間沒有空格,就會發生錯誤。那裏沒有有趣的非打印字符?嘗試重新輸入了一行:'字符串查詢= 「INSERT INTO racebikes VALUES(,,???)」;' – Glenn

回答

1

你可以嘗試值之前把三個字段

String query = "INSERT INTO racebikes (field1, field2, field3)" + "VALUES(?,?,?)"; 

與實際現場進行更換名。

+0

這也不能工作,這是我曾經嘗試過原始的方式。 – theGrayFox