INSERIR DADOS NUMA TABELA COM FOREIGN KEY
Boa tarde!
Pessoal,
se alguém pode me ajudar.
Tem duas tabelas do tipo "TB_FUNCIONARIO" , "TB_ENDERECO".
na tabela endereco tem foreign key da tabela funcionario, eu faço o insert na primeira e recupero o id que é gerado automático e com faço para inserir essa foreign key na tabela endereco? uma ve que a mesma não pode ser nulo.
segue o código abaixo que estou tentando fazer, agradeço desde já.
public void cadastrar(Funcionario funcionario) {
try {
Connection conexao = Conexao.obterConexao();
String sql1 = "INSERT INTO TB_FUNCIONARIO(NOME_FUNCIONARIO, CPF, TELEFONE, DT_NASCIMENTO) VALUES(?,?,?,?)";
PreparedStatement stm1 = conexao.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
stm1.setString(1, funcionario.getNome());
stm1.setString(2, funcionario.getCpf());
stm1.setString(3, funcionario.getTelefone());
stm1.setDate(4, new java.sql.Date(funcionario.getDt_nascimento().getTime()));
stm1.execute();
ResultSet rs = stm1.getGeneratedKeys();
int generatedKey = 0;
if (rs.next()) {
generatedKey = rs.getInt(1);
}
System.out.println("Inserted record's ID: " + generatedKey);
String sql2 = "INSERT INTO TB_ENDERECO(ID_FUNCIONARIO, RUA, BAIRRO, CEP)"
+ " VALUES(?,?,?,?)";
PreparedStatement stm2 = conexao.prepareStatement(sql2);
stm2.setString(1, funcionario.endereco.getRua());
stm2.setString(2, funcionario.endereco.getBairro());
stm2.setString(3, funcionario.endereco.getCep());
stm2.execute();
// stm1.close();
} catch (SQLException ex) {
Logger.getLogger(FuncionarioDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
Pessoal,
se alguém pode me ajudar.
Tem duas tabelas do tipo "TB_FUNCIONARIO" , "TB_ENDERECO".
na tabela endereco tem foreign key da tabela funcionario, eu faço o insert na primeira e recupero o id que é gerado automático e com faço para inserir essa foreign key na tabela endereco? uma ve que a mesma não pode ser nulo.
segue o código abaixo que estou tentando fazer, agradeço desde já.
public void cadastrar(Funcionario funcionario) {
try {
Connection conexao = Conexao.obterConexao();
String sql1 = "INSERT INTO TB_FUNCIONARIO(NOME_FUNCIONARIO, CPF, TELEFONE, DT_NASCIMENTO) VALUES(?,?,?,?)";
PreparedStatement stm1 = conexao.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
stm1.setString(1, funcionario.getNome());
stm1.setString(2, funcionario.getCpf());
stm1.setString(3, funcionario.getTelefone());
stm1.setDate(4, new java.sql.Date(funcionario.getDt_nascimento().getTime()));
stm1.execute();
ResultSet rs = stm1.getGeneratedKeys();
int generatedKey = 0;
if (rs.next()) {
generatedKey = rs.getInt(1);
}
System.out.println("Inserted record's ID: " + generatedKey);
String sql2 = "INSERT INTO TB_ENDERECO(ID_FUNCIONARIO, RUA, BAIRRO, CEP)"
+ " VALUES(?,?,?,?)";
PreparedStatement stm2 = conexao.prepareStatement(sql2);
stm2.setString(1, funcionario.endereco.getRua());
stm2.setString(2, funcionario.endereco.getBairro());
stm2.setString(3, funcionario.endereco.getCep());
stm2.execute();
// stm1.close();
} catch (SQLException ex) {
Logger.getLogger(FuncionarioDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
Mônica Pontes
Curtidas 0
Respostas
Raylan Zibel
30/04/2016
Não seria só passar "generatedKey" como primeiro parâmetro em "stm2.setInteger()"?
GOSTEI 0
Mônica Pontes
30/04/2016
fiz isso mais deu erro
GOSTEI 0
Raylan Zibel
30/04/2016
Qual o exato erro?
GOSTEI 0
Mônica Pontes
30/04/2016
o ID_FUUNCIONARIO não pode ser nulo.
GOSTEI 0
Raylan Zibel
30/04/2016
Tentar usar na posição 0, em vez de 1.
GOSTEI 0