2014-05-08 56 views
0

嗨,我目前正在開發一個應用程序來維護某個工廠的股票,我無法將訂單插入數據庫..我嘗試修復這個我自己的,但我不能這樣如果任何人可以幫助我很高興。Java MySQLSyntaxErrorException

因此,這裏是我的錯誤

com.mysql.jdbc.exceptions.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 'order (Add_Order_PID,Customer_idCustomer,Items_idItems,User_idUser,Color,Leather' at line 1 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723) 
at com.mysql.jdbc.Connection.execSQL(Connection.java:3250) 
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1355) 
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1270) 
at DB.Save_Update_Delete(DB.java:32) 
at New_Order1.jButton4ActionPerformed(New_Order1.java:605) 
at New_Order1.access$800(New_Order1.java:23) 
at New_Order1$9.actionPerformed(New_Order1.java:389) 
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.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289) 
at java.awt.Component.processMouseEvent(Component.java:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
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:729) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:688) 
at java.awt.EventQueue$3.run(EventQueue.java:686) 
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:702) 
at java.awt.EventQueue$4.run(EventQueue.java:700) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 
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) 

我的代碼是

for (int i = 0; i < jTable1.getRowCount(); i++) { 
      String $Name = "", $Color = "", $Leather = "", $s38 = "", $s39 = "", $s40 = "", $s41 = "", $s42 = "", $s43 = "", $s44 = "", $s45 = "", $s46 = "", Total = "", ItemId = ""; 
      $Name += jTable1.getValueAt(i, 0)+""; 
      $Color += jTable1.getValueAt(i, 1)+""; 
      $Leather += jTable1.getValueAt(i, 2)+""; 
      $s38 += jTable1.getValueAt(i, 3)+""; 
      $s39 += jTable1.getValueAt(i, 4)+""; 
      $s40 += jTable1.getValueAt(i, 5)+""; 
      $s41 += jTable1.getValueAt(i, 6)+""; 
      $s42 += jTable1.getValueAt(i, 7)+""; 
      $s43 += jTable1.getValueAt(i, 8)+""; 
      $s44 += jTable1.getValueAt(i, 9)+""; 

      $s45 += jTable1.getValueAt(i, 10)+""; 
      $s46 += jTable1.getValueAt(i, 11)+""; 
      Total += jTable1.getValueAt(i, 12)+""; 
      ItemId += jTable1.getValueAt(i, 13)+""; 
      DB.Save_Update_Delete("insert into order (Add_Order_PID,Customer_idCustomer,Items_idItems,User_idUser,Color,Leather,s38,s39,s40,s41,s42,s43,s44,s45,s46,Total) values ('"+pid+"','"+Integer.parseInt(customerID.getText())+"','"+Integer.parseInt(ItemId)+"','"+Integer.parseInt(UserID.getText())+"','"+$Color+"','"+$Leather+"','"+$s38+"','"+$s39+"','"+$s40+"','"+$s41+"','"+$s42+"','"+$s43+"','"+$s44+"','"+$s45+"','"+$s46+"','"+Total+"')"); 
     } 
+0

使用準備好的語句 –

+0

詳細說明「使用準備好的語句」:準備好的語句更有效,並且對SQL注入攻擊是安全的,而連接的SQL字符串(在您使用時)不是。 –

+0

@FrankSchmitt是他們在PreparedStatement中的任何方式來添加where條款下的條件,或者我需要首先確定所有條件,然後單步執行PreparedStatement。對不起,這是關閉的話題。 – guptakvgaurav

回答

0

您正在使用引號( '')

DB.Save_Update_Delete("insert into order (Add_Order_PID,Customer_idCustomer,Items_idItems,User_idUser,Color,Leather,s38,s39,s40,s41,s42,s43,s44,s45,s46,Total) values ('"+pid+"','"+Integer.parseInt(customerID.getText())+"','"+Integer.parseInt(ItemId)+"','"+Integer.parseInt(UserID.getText())+"','"+$Color+"','"+$Leather+"','"+$s38+"','"+$s39+"','"+$s40+"','"+$s41+"','"+$s42+"','"+$s43+"','"+$s44+"','"+$s45+"','"+$s46+"','"+Total+"')" 

); 

更好地利用PreparedStatementlearn插入INT值從這裏開始

+0

不確定這是否是問題,但通常SQL服務器將足夠智能取int的字符串值(單引號)。 – Sky

+0

@Shashi我看到了,雖然我用Oracle和MSSQL進行了測試,但將整個單引號括起來並不存在問題。然而對於MYSQL,我正在閱讀[this](http://stackoverflow.com/questions/4918512/disadvantages-of-quoting-integers-in-a-mysql-query),並注意到那裏的OP比較了引用的整數所以我猜它至少適用於他的MySQL版本。 – Sky

+0

對不起@天空你是對的.. – Shashi

0

檢查Add_Order_PID是否是一個整數。如果是整數,不要放在雙引號中。

對於任何數字數據類型,不要把雙引號內..

最好使用PreparedStatement的插入

+0

我刪除了雙引號,但他卻得到相同的錯誤。旁邊,當我刪除雙引號它看起來像一個字符串 – Shansl95

+0

我也嘗試了準備聲明,但它給出了相同的錯誤 – Shansl95

+0

請提供表結構..使用pastebin.com並提供網址..它會更好或更新問題與表結構 – Shashi

0
insert into order (Add_Order_PID,Customer_idCustomer,Items_idItems,User_idUser,Color,Leather,s38,s‌​39,s40,s41,s42,s43,s44,s45,s46,Total) values ('8','1','2','1','Black','Leather 1','2','0','0','0','0','0','0','0','0','2') 

直接在MySQL控制檯試試你的SQL語句。它會給你更多關於真正問題的信息。也許你正在違反一些外鍵。

相關問題