Java SQL更新语法错误
Java SQL update syntax error
我的服务器上有一个函数 "update",我在客户端上通过 "btnUpdateActionPerformed" 调用它。我正在对两个表执行更新,即 "patient" 和 "insurance" 但是当我 运行 它时,服务器端显示以下错误:
Error1: 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 'WHERE patient_id = 3' at line 1
更新代码如下:
public String update(int patient_id, String first_name ,String last_name, String date_of_birth, String gender, String address, int contact_number,
String occupation, String nic_number, String marital_status, String r_first_name, String r_last_name, String r_occupation, int r_contact_number,
int room_number,int doctor_id_assigned,int nurse_id_assigned,int company_id,String company_name,String company_address,
int phone_number, String policy_group) throws RemoteException{
try{
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost/hospital","root","");
String patient_sql = "UPDATE patient SET first_name = ?, last_name = ?, date_of_birth = ?, gender = ?, address = ?, contact_number = ?,"
+ " occupation = ?, nic_number = ?, marital_status = ?, r_first_name = ?, r_last_name = ?, r_occupation = ?, r_contact_number = ?, "
+ "room_number = ?, doctor_id_assigned = ?, nurse_id_assigned = ?, company_id = ?, WHERE patient_id = ?";
ps=con.prepareStatement(patient_sql);
ps.setString(1, first_name);
ps.setString(2, last_name);
ps.setString(3, date_of_birth);
ps.setString(4, gender);
ps.setString(5, address);
ps.setInt(6, contact_number);
ps.setString(7, occupation);
ps.setString(8, nic_number);
ps.setString(9, marital_status);
ps.setString(10, r_first_name);
ps.setString(11, r_last_name);
ps.setString(12, r_occupation);
ps.setInt(13, r_contact_number);
ps.setInt(14, room_number);
ps.setInt(15, doctor_id_assigned);
ps.setInt(16, nurse_id_assigned);
ps.setInt(17, company_id);
ps.setInt(18, patient_id);
ps.executeUpdate();
String insurance_sql = "UPDATE insurance SET company_name = ?, company_address = ?, phone_number = ?, policy_group = ? WHERE company_id = ?";
ps=con.prepareStatement(insurance_sql);
ps.setString(1, company_name);
ps.setString(2, company_address);
ps.setInt(3, phone_number);
ps.setString(4, policy_group);
ps.setInt(5, company_id);
ps.executeUpdate();
con.close();
}
catch(SQLException e){
System.out.println("Error1: "+e);
}
catch(ClassNotFoundException e){
System.out.println("Error2: "+e);
}
return "updated";
}
btnUpdateActionPerformed 的代码:
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt){
try{
Registry reg = LocateRegistry.getRegistry("localhost",5022);
ClerkInterface ci = (ClerkInterface)reg.lookup("clerk");
int patient_id = Integer.parseInt(txtPatientId.getText());
String first_name = txtFirstName.getText();
String last_name = txtLastName.getText();
String date_of_birth = txtDateOfBirth.getText();
String gender="";
if(radMale.isSelected()){
gender = "Male";
}
else if(radFemale.isSelected()){
gender = "Female";
}
String address = txtAddress.getText();
int contact_number = Integer.parseInt(txtContactNumber.getText());
String occupation = txtOccupation.getText();
String nic_number = txtNICNumber.getText();
String marital_status = txtNICNumber.getText();
String r_first_name = txtRFirstName.getText();
String r_last_name = txtRLastName.getText();
String r_occupation = txtROccupation.getText();
int r_contact_number = Integer.parseInt(txtRContactNumber.getText());
int room_number = Integer.parseInt(txtRoomNumber.getText());
int doctor_id_assigned = Integer.parseInt(txtDoctorIdAssigned.getText());
int nurse_id_assigned = Integer.parseInt(txtNurseIdAssigned.getText());
int company_id = Integer.parseInt(txtCompanyId.getText());
String company_name = txtCompanyName.getText();
String company_address = txtCompanyAddress.getText();
int phone_number = Integer.parseInt(txtCompanyPhoneNumber.getText());
String policy_group = txtPolicyGroup.getText();
ci.update(patient_id, first_name, last_name, date_of_birth, gender, address, contact_number, occupation, nic_number, marital_status,
r_first_name, r_last_name, r_occupation, r_contact_number, room_number, doctor_id_assigned, nurse_id_assigned, company_id,
company_name, company_address, phone_number, policy_group);
JOptionPane.showMessageDialog(null,"Patient updated");
}
catch(RemoteException e){System.out.println("Error: "+e.getMessage());}
catch(NotBoundException e){System.out.println("Error: "+e.getMessage());}
}
去掉company_id = ?,
后面的逗号
我的服务器上有一个函数 "update",我在客户端上通过 "btnUpdateActionPerformed" 调用它。我正在对两个表执行更新,即 "patient" 和 "insurance" 但是当我 运行 它时,服务器端显示以下错误:
Error1: 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 'WHERE patient_id = 3' at line 1
更新代码如下:
public String update(int patient_id, String first_name ,String last_name, String date_of_birth, String gender, String address, int contact_number,
String occupation, String nic_number, String marital_status, String r_first_name, String r_last_name, String r_occupation, int r_contact_number,
int room_number,int doctor_id_assigned,int nurse_id_assigned,int company_id,String company_name,String company_address,
int phone_number, String policy_group) throws RemoteException{
try{
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost/hospital","root","");
String patient_sql = "UPDATE patient SET first_name = ?, last_name = ?, date_of_birth = ?, gender = ?, address = ?, contact_number = ?,"
+ " occupation = ?, nic_number = ?, marital_status = ?, r_first_name = ?, r_last_name = ?, r_occupation = ?, r_contact_number = ?, "
+ "room_number = ?, doctor_id_assigned = ?, nurse_id_assigned = ?, company_id = ?, WHERE patient_id = ?";
ps=con.prepareStatement(patient_sql);
ps.setString(1, first_name);
ps.setString(2, last_name);
ps.setString(3, date_of_birth);
ps.setString(4, gender);
ps.setString(5, address);
ps.setInt(6, contact_number);
ps.setString(7, occupation);
ps.setString(8, nic_number);
ps.setString(9, marital_status);
ps.setString(10, r_first_name);
ps.setString(11, r_last_name);
ps.setString(12, r_occupation);
ps.setInt(13, r_contact_number);
ps.setInt(14, room_number);
ps.setInt(15, doctor_id_assigned);
ps.setInt(16, nurse_id_assigned);
ps.setInt(17, company_id);
ps.setInt(18, patient_id);
ps.executeUpdate();
String insurance_sql = "UPDATE insurance SET company_name = ?, company_address = ?, phone_number = ?, policy_group = ? WHERE company_id = ?";
ps=con.prepareStatement(insurance_sql);
ps.setString(1, company_name);
ps.setString(2, company_address);
ps.setInt(3, phone_number);
ps.setString(4, policy_group);
ps.setInt(5, company_id);
ps.executeUpdate();
con.close();
}
catch(SQLException e){
System.out.println("Error1: "+e);
}
catch(ClassNotFoundException e){
System.out.println("Error2: "+e);
}
return "updated";
}
btnUpdateActionPerformed 的代码:
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt){
try{
Registry reg = LocateRegistry.getRegistry("localhost",5022);
ClerkInterface ci = (ClerkInterface)reg.lookup("clerk");
int patient_id = Integer.parseInt(txtPatientId.getText());
String first_name = txtFirstName.getText();
String last_name = txtLastName.getText();
String date_of_birth = txtDateOfBirth.getText();
String gender="";
if(radMale.isSelected()){
gender = "Male";
}
else if(radFemale.isSelected()){
gender = "Female";
}
String address = txtAddress.getText();
int contact_number = Integer.parseInt(txtContactNumber.getText());
String occupation = txtOccupation.getText();
String nic_number = txtNICNumber.getText();
String marital_status = txtNICNumber.getText();
String r_first_name = txtRFirstName.getText();
String r_last_name = txtRLastName.getText();
String r_occupation = txtROccupation.getText();
int r_contact_number = Integer.parseInt(txtRContactNumber.getText());
int room_number = Integer.parseInt(txtRoomNumber.getText());
int doctor_id_assigned = Integer.parseInt(txtDoctorIdAssigned.getText());
int nurse_id_assigned = Integer.parseInt(txtNurseIdAssigned.getText());
int company_id = Integer.parseInt(txtCompanyId.getText());
String company_name = txtCompanyName.getText();
String company_address = txtCompanyAddress.getText();
int phone_number = Integer.parseInt(txtCompanyPhoneNumber.getText());
String policy_group = txtPolicyGroup.getText();
ci.update(patient_id, first_name, last_name, date_of_birth, gender, address, contact_number, occupation, nic_number, marital_status,
r_first_name, r_last_name, r_occupation, r_contact_number, room_number, doctor_id_assigned, nurse_id_assigned, company_id,
company_name, company_address, phone_number, policy_group);
JOptionPane.showMessageDialog(null,"Patient updated");
}
catch(RemoteException e){System.out.println("Error: "+e.getMessage());}
catch(NotBoundException e){System.out.println("Error: "+e.getMessage());}
}
去掉company_id = ?,