Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:20:12+00:00 2026-05-27T13:20:12+00:00

What I was trying to do is that when I click the Add button

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:20:13+00:00Added an answer on May 27, 2026 at 1:20 pm

    The following is the tables, I can assume from your question.

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

    And I assume the following relationship:

    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.
    

    From Wikipedia on Foreign Key:

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

    If I understand your table structure correctly, then it put me to the following conclusion.

    You are inserting a new record to personal_info table, but you omit the value for idpersonal_info since it is primary and auto_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 of idpersonal_info exists in idusers field of users table. Since you don’t specify a value for idpersonal_info, MySQL can take either NULL or an auto incremented value for idpersonal_info, which one will be taken by MySQL is I am not sure. If it takes NULL, then it will not be on users table, 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 the users table, 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_info also in your INSERT query. You have to make sure that exists in the users table also.

    Hope that helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create buttons for ckeditor that will add rows and columns
I am trying to add click event to one of the widgets that use
I am trying to add a click event on the element that is return
I am trying to add a button inside a jqGrid column and to assign
I'm trying to create an app in Visual Basic that will allow a user
I am trying to make a jquery menu that when I click on one
I am trying to make a div, that when you click it turns into
I'm trying to make a window that closes when you click outside it ,
I'm trying to track down a bug that occurs when I click a particular
I am trying to make it so that a user can ctrl-click outside of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.