foreign key registration method

Asked

Viewed 37 times

-1

I’m having trouble with the foreign key. I have a company record and in this register I put 3 foreign keys, which is that such relationship 1:n, right?

Anyway, I can only put the columns on the table itself, but when it’s the foreign key I can’t.

//Metodo de cadastro 

public void cadastroempresa(CadastroEmpresa Cadastro){
SQL....
prepared...

pst.setString(1, Cadastro.getNomeEmpresa());
    pst.setString(2, Cadastro.getCnpjEmpresa());
    pst.setString(3, Cadastro.getNomeResponsavel());
    // A linha abaixo é a fk de outra tabela que tá dando erro.
    // Essa fk tá dentro de um combobox já preenchido com os dados 
    pst.setInt(4,Cadastro.getfkestado());
}
  • but what’s the mistake?

  • Do you agree that "making a mistake" offers little chance for anyone to try to help you? What error? Which error message appears to you?

  • I was able to fix it, I wasn’t able to put the foreign key in the method

1 answer

0


1 - The first step to check is if the value you are picking up from the combobox is correct. You can do this checking by assigning value to a variable and by debugging you see what the value of the variable is. A simpler and faster option to check would be to use System.out.println(): System.out.println(Cadastro.getfkestado());.

2 - If after checking the value you are getting from the combobox is really correct, check if the value you are passing as a foreign key really exists in the other table. For example, if you made relationship between a table of companies and a table of state. In the company table the key value of the state table will be inserted. If you register a company from Minas Gerais for example, and the state of Minas Gerais has the key with the value 4 in the table of states, you must enter the value 4 in the table of company. So, check if the value you are passing to the company table exists in the state table.

I await your reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.