0

I have a function "update" on my server and I call it through "btnUpdateActionPerformed" on my client. I'm performing an update on two tables, namely "patient" and "insurance" but when I run it, the following error is displayed on server side:

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

Below are the codes for update:

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";
}

The codes for 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());}
}     

1 Answer 1

1

Remove the comma after company_id = ?,

Sign up to request clarification or add additional context in comments.

1 Comment

@user3162879 No i am not a genius. have made these errors by my self ;)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.