1
I created a table license plate, and 5 tables of materials, both are linked by foreign key, and within my program has a screen to simulate a student’s enrollment, where one of the steps is to choose the subjects, being able to choose at most 5 and at least one. However I am not managing to do the proper error handling for when choosing from first, Q4 materials.
Follow 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;
}
However if I do not select the 5 materials, it returns me the following error:
"Error executing SQLERRO command: tabéla insertion or update license plate violates foreign key restriction"
I believe that the error happens because in the enrollment table there are 5 fields with foreign key of the materials, which in fact according to the tables should all be filled. What I could do is exchange && for || and say that it is necessary to choose 5 materials, but it would not simulate a real scenario.
How can I do this treatment so that by choosing at least one material it is possible to do the matricula?
May I present you
JCheckBox
. It makes much more sense to use this component, so you only validate if you have at least one checked item. See a example– user28595
Post the DDL of your tables too, if your FK is as not null in the table plate, you will have to enter the 5 fields, otherwise you will not be able to write.
– Geferson
I believe there’s a mistake in your bank logic. I imagine that the relation between matricula and materia is nxn, that is, there can be materials with 0 or n matriculas, and vice versa. Therefore, I would need an auxiliary table to make this relationship. Foreign key is used when the relation is 1xn, table n is the one that will have a foreign key.
– user28595
Diego, really I didn’t know the jCheckBox component yet, I took a look at the example, thank you.
– Edivan M.N
Geferson, Really. the FK were as not null, there was one of the mistakes...was worth
– Edivan M.N
Diego, referring to the logic of the bank, I really am beginner in BD, but the tables were not nxn, what happened was that I created 5 tables where in one was enough, because the 5 tables of subjects had the same subjects in the 5, but I managed to solve the problem, I am already deepening more here in the relationship issue between tabélas, thanks for the force
– Edivan M.N
I’ve already checked several websites, including international ones, for an answer on how to deal with this error. But fortunately I found it very simply in this link: http://javafree.uol.com.br/topic-883230-Ajuda-com-violacao-de-chave-estrangeira.html. I hope you help everyone.
– smg