entrepreneurSam
entrepreneurSam

Reputation: 3

I got Exception from MySql and it won't Update table?

When I pass the button on click event, with all Jcomponats value, It won't update my table instead of that It reflect exception while my code is working properly in other table?

private void update() {
    String id = txtPcno.getText();
    String updateQuery = "insert into n_patient (name, gender, NRIC, nationality, height, weight, smoke, last_seen) values(?,?,?,?,?,?,?,?)";
    try {
        pst = con.prepareStatement(updateQuery);

        //pst.setString(1, txtPcno.getText());
        pst.setString(1, txtName.getText());
        pst.setString(2, txtGender.getText());
        pst.setString(3, txtNRIC.getText());
        pst.setString(4, txtNationality.getText());
        pst.setString(5, txtHeight.getText());
        pst.setString(6, txtWeight.getText());
        pst.setString(7, txtSmoke.getText());
        pst.setString(8, txtLastSeen.getText());

        int rowAffected = pst.executeUpdate();

        JOptionPane.showMessageDialog(null, String.format(" %d Modified Records Are Saved ", rowAffected));
        clear();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here is the Image of indication Problem

Upvotes: 0

Views: 41

Answers (1)

Vaibhav p prajapati
Vaibhav p prajapati

Reputation: 38

You are getting this exception because,

  • While inserting a record into that table, you do not insert data for that('Pcno') column and this column having a property like the 'not nullable' and 'primary key'.

    • In this scenario two option to solve that issue,
      1. Make that column as auto-increment column, Or
      2. While inserting the record into that table also pass the value of the 'Pcno' column

Upvotes: 1

Related Questions