2011-12-09 23 views
0

我想要做的是,當我點擊添加按鈕時,它會創建另一個實例,在那個實例內部有一個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,如果你們想知道爲什麼我有一個外鍵,我有一個外鍵,所以,如果我刪除行,其他表中的所有其他行將被刪除,但它不起作用。

+0

您可以編輯排長短 – Jomoos

+0

在那裏,我編輯它。請重新檢查我的問題 – user962206

回答

1

以下是表格,我可以從您的問題中假設。

users   -- idusers, other fields 
personal_info -- idpersonal_info, First_Name, Middle_Initial, Last_Name, 
        Date_Of_Birth, Marital_Status, Beneficiaries 

而且我認爲以下關係:

idusers   - primary key 
idpersonal_info - foreign key referencing idusers, 
        also, it is set to ON DELETE CASCADE, ON UPDATE CASCADE 

        You had set it as primary key with AUTO_INCREMENT also. 

維基百科上Foreign Key

The values in one row of the referencing columns 
must occur in a single row in the referenced table. 

如果我正確理解你的表結構,然後把我下面的結論。

您正在爲personal_info表插入新記錄,但您省略了idpersonal_info的值,因爲它是主要的,並且auto_increment。現在,在插入該行之前,MySQL將對您設置的外部約束進行檢查。在那裏,它檢查idpersonal_info的值是否存在於字段的users表中。由於您沒有爲idpersonal_info指定值,因此MySQL可以採取NULLidpersonal_info的自動遞增值,MySQL將採取哪一個值,我不確定。如果它需要NULL,那麼它將不會在users表中,因此違反了外鍵約束。如果它需要一個auto_incremented值,那麼它有可能不會出現在users表中,如果不存在,則違反外鍵約束。

現在,我能想到的解決方案是,您還必須在INSERT查詢中指定idpersonal_info的值。您還必須確保存在於users表中。

希望幫助:)

+0

它確實有幫助!抱歉給予一個很晚的反饋和答案。但這真的有幫助! – user962206

+1

@ user962206歡迎您! – Jomoos