0

i tried writing a code, that would update a record when a button is pressed with any changes made to any fields. it displays an error message though. this is the code:

    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);

The connection to the db and stuff all works, so it must be just the syntax, as this is the error message i am getting:

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)

1 Answer 1

2

you have error here, you forget = sign:

 ", Phone" + textField_8.getText() 

also you opening ( without closing , and actually you dont need it:

set (StudentID

you need to put String in between quotes 'name' in case there are names with spaces, like:

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()) + "'";

you can use String.format, more readable and you can detect error easily:

String statement = String.format("UPDATE student set studentId='%s', firstName='%s'",
id, name);
Sign up to request clarification or add additional context in comments.

1 Comment

its still giving me an error message saying that the syntax is wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.