Error Handling for foreign key violation-java-postgreSql

1

I have created a table enrollment , and 5 subject tables, the two are linked by foreign key, and inside my program has a screen to simulate a student enrollment, where one of the steps is choose the disciplines, being able to choose at most 5 and at least one. But I'm not able to make the proper error handling for when you choose first, q4 matters.

The following is the treatment in the code:

if(jComboBoxMateria1.getSelectedIndex() == 0 && jComboBoxMateria2.getSelectedIndex() == 0 && jComboBoxMateria3.getSelectedIndex() == 0 &&
                    jComboBoxMateria4.getSelectedIndex() == 0 && jComboBoxMateria5.getSelectedIndex() == 0){
                JOptionPane.showMessageDialog(null, "É Necessário escolher ao menos 1 Matéria para efetuar a Matricula");
                return;
            }

But if I do not select the 5 materials, it returns the following error:

  

"Error Running SQLERRO Command: Insert or Update in Table   enrollment violates foreign key constraint "

I believe that the error happens because in the table enrollment has the 5 fields with foreign key of the subjects, which in fact as the tables should be all filled. What I could do is change the & by || and say that it is necessary to choose 5 subjects, but would not simulate a real scenario.

How can I make this treatment so that choosing at least one subject is already possible to enroll?

    
asked by anonymous 02.05.2016 / 09:58

0 answers