Home > front end >  derbysqlintegrityconstraintviolationexception cause a violation of foreign key
derbysqlintegrityconstraintviolationexception cause a violation of foreign key

Time:08-08

I am using netbeans to create a gui. I created two tables, PETOWNER and ANIMALREG where the primary key of table PETOWNER is PETOWNERNIC and is also the foreign key of table ANIMALREG. Now when i have to insert a record in table ANIMALREG i get an error: derbysqlintegrityconstraintviolationexception: INSERT on table 'ANIMALREG' caused a violation of foreign key constraint 'SQLxxx' for key(). The statement has been rolled back. Here's my table of PETOWNER:

CREATE TABLE "PETOWNER" (
"PETOWNERNIC" varchar(14) NOT NULL,
"FIRSTNAME" varchar(20) NOT NULL,
"LASTNAME" varchar(30) NOT NULL,
"ADDRESS1" varchar(20) NOT NULL,
"ADDRESS2" varchar(20) NOT NULL,
"ADDRESS3" varchar(20) NOT NULL,
"PHONE" varchar(8) NOT NULL,
PRIMARY KEY ("PETOWNERNIC")
);

This is my table for ANIMALREG:

CREATE TABLE "ANIMALREG" (
"PETID" VARCHAR(5) NOT NULL,
"PETNAME" VARCHAR(20) NOT NULL,
"PETOWNERNIC" VARCHAR(14) NOT NULL,
"BREED" VARCHAR(30),
"SEX" VARCHAR(1) NOT NULL,
"DOB" VARCHAR(10),
"WEIGHT" VARCHAR(5) NOT NULL,
"VACCINE" VARCHAR(100) NOT NULL,
PRIMARY KEY ("PETID"),
FOREIGN KEY ("PETOWNERNIC") REFERENCES 
"PETOWNER"("PETOWNERNIC")
);

And my code where i can insert a new record:

private void addNewPetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // To insert a new pet record
        try{
            String sqlInsertpet = "insert into ANIMALREG (PETID, PETNAME,PETOWNERNIC, BREED,SEX,DOB,WEIGHT, VACCINE) values(?,?,?,?,?,?,?,?)";
            ps = con.prepareStatement(sqlInsertpet);
            ps.setString(1, PetIDTxt.getText());
            ps.setString(2, PetNameTxt.getText());
            ps.setString(3, PetOwnerNicTxt.getText());
            ps.setString(4, PetBreedTxt.getText());
            ps.setString(5, PetSexTxt.getText());
            ps.setString(6, PetDobTxt.getText());
            ps.setString(7, PetWeightTxt.getText());
            ps.setString(8, PetVaccineTxt.getText());
            ps.execute();
            JOptionPane.showMessageDialog(null, "New Pet added !!\nSuccess");
            updatePetTable();
            PetIDTxt.setText("");
            PetNameTxt.setText("");
            PetOwnerNicTxt.setText("");
            PetBreedTxt.setText("");
            PetSexTxt.setText("");
            PetDobTxt.setText("");
            PetWeightTxt.setText("");
            PetVaccineTxt.setText("");
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Insert New Pet Error: " e);
        }
    }                                               

Can someone tell me what i am missing?

CodePudding user response:

I figured it myself. My code are well written. I made a mistake in variable name where i used the variable name of table PETOWNER in my code to insert a new pet record.

  • Related