INSERIR DADOS NUMA TABELA COM FOREIGN KEY
30/04/2016
0
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
Curtir tópico
+ 0
Responder
Posts
30/04/2016
Raylan Zibel
Não seria só passar "generatedKey" como primeiro parâmetro em "stm2.setInteger()"?
Responder
Clique aqui para fazer login e interagir na Comunidade :)