繁体   English   中英

java mysql executeupdate更新

[英]java mysql executeupdate update

我尝试编写一个代码,当按下按钮并对任何字段进行任何更改时,该代码将更新记录。 它会显示错误消息。 这是代码:

    JButton button = new JButton("Save");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {         

        try {
            con.stmt.executeUpdate("UPDATE student set (StudentID=" + txtAUniqueStudent.getText() + ", FirstName=" + textField_1.getText() + ", Surname=" + textField_2.getText() + ", DOB=" + textField_3.getText() + ", Gender=" + textField.getText() + ", AddressLine1=" + textField_4.getText() + ", AddressLine2=" + textField_5.getText() + ", PostCode=" + textField_6.getText() + ", FatherFullName=" + textField_7.getText() + ", Phone" + textField_8.getText() + ", Mobile=" + textField_9.getText() + ", Fax=" + textField_10.getText() + ", Email=" + textField_11.getText() + ", EmergencyContactName=" + textField_12.getText() + ", EmergencyTel=" + textField_13.getText() + ", AcademicYear=" + textField_14.getText() + ", Subjects=" + textField_15.getText() + "WHERE studentID =" + textField_16.getText());
            con.stmt.executeUpdate();
            JOptionPane.showMessageDialog(frmFindStudent, "Record has been updated successfully.");
        } 

        catch (SQLException e) {
        //System.out.println("Record couldn't be added!");
        e.printStackTrace();
        JOptionPane.showMessageDialog(frmFindStudent, "Record couldn't be updated. Please try again.");
        }
        }
        });
    button.setBounds(333, 517, 89, 23);
    panel_1.add(button);

与db和stuff的连接都有效,因此它必须只是语法,因为这是我得到的错误消息:

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 '(StudentID=25415, FirstName=Shangeethan, Surname=Seevaratnam, DOB=30.08.1994, Ge' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1749)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1666)
at FindStudent$2.actionPerformed(FindStudent.java:255)
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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)

你在这里有错误,你忘了=签名:

 ", Phone" + textField_8.getText() 

你打开(没有关闭,实际上你不需要它:

set (StudentID

你需要把String放在引号'name'之间,以防有空格的名字,比如:

con.stmt.executeUpdate("UPDATE student set StudentID='" + txtAUniqueStudent.getText() + "', FirstName='" + textField_1.getText() + "', Surname='" + textField_2.getText() + "', DOB='" + textField_3.getText() + "', Gender='" + textField.getText() + "', AddressLine1='" + textField_4.getText() + "', AddressLine2='" + textField_5.getText() + "', PostCode='" + textField_6.getText() + "', FatherFullName='" + textField_7.getText() + "', Phone='" + textField_8.getText() + "', Mobile='" + textField_9.getText() + "', Fax='" + textField_10.getText() + "', Email='" + textField_11.getText() + "', EmergencyContactName='" + textField_12.getText() + "', EmergencyTel='" + textField_13.getText() + "', AcademicYear='" + textField_14.getText() + "', Subjects='" + textField_15.getText() + "' WHERE studentID ='" + textField_16.getText()) + "'";

你可以使用String.format,更具可读性,你可以轻松地检测错误:

String statement = String.format("UPDATE student set studentId='%s', firstName='%s'",
id, name);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM