我想要做的是,當我點擊添加按鈕時,它會創建另一個實例,在那個實例內部有一個QUERY,它將與數據庫交互,添加我得到的輸入從JTextField中,我有它不斷給我一個帶查詢的Actionevent不起作用
m.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
這裏的另一個問題是我動作事件代碼
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);
}
}
我addEmployee代碼
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();
}
}
這裏是錯誤
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)
,如果你們想知道爲什麼我有國外的名單,是我添加了一個外鍵,它和它的外鍵是idUsers,如果你們想知道爲什麼我有一個外鍵,我有一個外鍵,所以,如果我刪除行,其他表中的所有其他行將被刪除,但它不起作用。
您可以編輯排長短 – Jomoos
在那裏,我編輯它。請重新檢查我的問題 – user962206