gpt4 book ai didi

java mysql 执行更新更新

转载 作者:行者123 更新时间:2023-11-29 08:57:27 26 4
gpt4 key购买 nike

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

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

与数据库和其他内容的连接都有效,所以它一定只是语法,因为这是我收到的错误消息:

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

您需要将字符串放在引号“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);

关于java mysql 执行更新更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com