2015-05-17 28 views
1

我想用下面的代碼更新一個表,並將ID用作主鍵,但只發生下面的錯誤。在Eclipse中執行SQL查詢時出錯

那是什麼香港專業教育學院在我的actionPerformed: 我PrimaryKey的是 「ID」

public void actionPerformed(ActionEvent e) { 
      try { 
       String query="Update MitarbeiterInfo set " 
         + "Vorname='"+textField.getText()+"' ," 
         + "Nachname='"+textField_1.getText()+"' ," 
         + "Geburtsdatum='"+textField_2.getText()+"' ," 
         + "Wohnadresse='"+textField_3.getText()+"' ," 
         + "Postleitzahl='"+textField_4.getText()+"'," 
         + "Eintrittsdatum='"+textField_5.getText()+"'," 
         + "Handynummer='"+textField_6.getText()+"'," 
         + "Email='"+textField_7.getText()+"' " 
         + "ID='"+fieldID.getText()+"' " 
         + "where ID='"+fieldID.getText()+"' "; 

       PreparedStatement pst=connection.prepareStatement(query); 


       pst.execute(); 

       JOptionPane.showMessageDialog(null, "Mitarbeiter aktualisiert"); 

       pst.close(); 
      } catch (Exception b) { 
       b.printStackTrace(); 
      } 
     } 

的錯誤發生:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "ID": syntax error) 
at org.sqlite.DB.newSQLException(DB.java:886) 
at org.sqlite.DB.newSQLException(DB.java:897) 
at org.sqlite.DB.throwex(DB.java:864) 
at org.sqlite.NativeDB.prepare(Native Method) 
at org.sqlite.DB.prepare(DB.java:207) 
at org.sqlite.PrepStmt.<init>(PrepStmt.java:50) 
at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:616) 
at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:606) 
at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:578) 
at GUI$2.actionPerformed(GUI.java:139) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

請幫助我。 感謝

回答

0

您更新ID列(你不真正需要做的)前錯過了上一個逗號:

+ "Email='"+textField_7.getText()+"', " 
+ "ID='"+fieldID.getText()+"' " 
+ "where ID='"+fieldID.getText()+"' "; 

而且你不需要在這種情況下,使用PreparedStatement(無參數綁定到準備好的查詢)。只需使用一個Statement對象:

Statement statement = connection.createStatement(); 
statement.executeUpdate(query); 
+0

謝謝你你幫我:) – rubine

0

刪除

+ "ID='"+fieldID.getText()+"' "

你不需要它,當你有

"where ID='"+fieldID.getText()+"' ";

它僅會自動更新到相同的價值。

+0

謝謝:)... – rubine