What I was trying to do is that when I click the Add button it will create another instance where inside that instance there’s a QUERY that it will interact with the database, to add the input that I got from the JTextFields, and I have another problem it keeps on giving me an
m.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
here is my action event code
private class AddHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
Personal personal = new Personal(firstTxt.getText(),miTxt.getText(),
lastTxt.getText(),dob.getText(),maritalTxt.getText(),beneTxt.getText());
Contact contact = new Contact(telTxt.getText(),addTxt.getText(),
mobTxt.getText(),emailTxt.getText());
Employee employee = new Employee(posTxt.getText(),payTTxt.getText(),payRTxt.getText(),hireTxt.getText());
Finance finance = new Finance();
finance.addEmployee(personal,contact,employee);
}
}
My addEmployee code
public void addEmployee(Personal p ,Contact c,Employee e) {
Connection conn = Jdbc.dbConn();
Statement statement = null;
String insert1 = "INSERT INTO personal_info (`First_Name`, `Middle_Initial`, `Last_Name`, `Date_Of_Birth`, `Marital_Status`, `Beneficiaries`) VALUES ('"+p.getFirstName()+"', '"+p.getMiddleInitial()+"'" +
" , '"+p.getLastName()+"', '"+p.getDateOfBirth()+"', '"+p.getMaritalStatus()+"', '"+p.getBeneficiaries()+"')";
try{
statement = conn.createStatement();
statement.executeUpdate(insert1);
statement.close();
conn.close();
JOptionPane.showMessageDialog(null, "Employee Added!!");
}catch(Exception ex){
ex.printStackTrace();
}
}
here is the list of errors
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`finalpayroll`.`personal_info`, CONSTRAINT `personal_info_ibfk_1` FOREIGN KEY (`idpersonal_info`) REFERENCES `users` (`idusers`) ON DELETE CASCADE ON UPDATE CASCADE)
if you guys are wondering why I have foreign, is that I added a foreign key to it and it’s foreign key is idUsers, if you guys are wondering why I have a foreign key, I have a foreign key so that, if I Delete row, all of the other rows in the other tables will be deleted but it doesn’t work.
The following is the tables, I can assume from your question.
And I assume the following relationship:
From Wikipedia on Foreign Key:
If I understand your table structure correctly, then it put me to the following conclusion.
You are inserting a new record to
personal_infotable, but you omit the value foridpersonal_infosince it is primary andauto_increment. Now, before insertion of the row, MySQL will perform a check on the foreign constraint you set. There, it checks to see, whether the value ofidpersonal_infoexists inidusersfield ofuserstable. Since you don’t specify a value foridpersonal_info, MySQL can take eitherNULLor an auto incremented value foridpersonal_info, which one will be taken by MySQL is I am not sure. If it takesNULL, then it will not be onuserstable, and hence the violation of foreign key constraint. If it takes an auto_incremented value, there is a chance that it will not present in theuserstable, and if not present, violates foreign key constraint.Now, the solution which I can think of is that, you have to specify a value for
idpersonal_infoalso in yourINSERTquery. You have to make sure that exists in theuserstable also.Hope that helps 🙂