【问题标题】:Error while executing SQL query in Eclipse在 Eclipse 中执行 SQL 查询时出错
【发布时间】:2015-05-17 17:30:24
【问题描述】:

我想用下面的代码更新一个表,我使用 ID 作为主键,但只发生下面的错误。

这就是我的 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)

请帮助我。 谢谢

【问题讨论】:

    标签: java sql database eclipse


    【解决方案1】:

    您在更新 ID 列之前错过了最后一个逗号(您实际上并不需要这样做):

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

    在这种情况下,您也不需要使用PreparedStatement(没有参数绑定到准备好的查询)。只需使用Statement 对象:

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

    【讨论】:

      【解决方案2】:

      删除

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

      你不需要它,当你有的时候

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

      它只会将自己更新为相同的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-18
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        相关资源
        最近更新 更多