Insert in mysql database with java

Asked

Viewed 537 times

1

I’m trying to give 6 insert in 6 tables, but I did not succeed that way, someone can help me with a better idea?

 private void salvarActionPerformed(java.awt.event.ActionEvent evt) {                                       

     DadosFuncionario cv = new DadosFuncionario();

     try {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con;

         con = DriverManager.getConnection("jdbc:mysql://localhost/dp","root","root");

         String query = "insert into funcionario (cargoOcupado,nome,cpf,rg,orgaoExped,dataExped,dataDeNascimento,email"
            + "estadoCivil,sexo,cnh,titulo,zona,secao,carteiraTrabalho,serieCtrabalho,cartResevista,serieResevista"
            + "racaCor,assinatura,pis) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    PreparedStatement stmt = con.prepareStatement(query);

    stmt.setString(1, cargoOcupado.getText());
    stmt.setString(2, nome.getText());
    stmt.setString(3, cpf.getText());
    stmt.setString(4, rg.getText());
    stmt.setString(5, orgaoExped.getText());
    stmt.setString(6, ((JTextField)dataExped.getDateEditor().getUiComponent()).getText());
    stmt.setString(7, ((JTextField)dataDeNascimento.getDateEditor().getUiComponent()).getText());
    stmt.setString(8, email.getText());
    //stmt.setIcon(10, foto.getIcon);
    stmt.setString(9, (String) estadoCivil.getSelectedItem());
    stmt.setString(10, (String) sexo.getSelectedItem());
    stmt.setString(11, cnh.getText());
    stmt.setString(12, titulo.getText());
    stmt.setString(13, zona.getText());
    stmt.setString(14, secao.getText());
    stmt.setString(15, carteiraTrabalho.getText());
    stmt.setString(16, serieCtrabalho.getText());
    stmt.setString(17, cartResevista.getText());
    stmt.setString(18, serieResevista.getText());
    stmt.setString(19, (String) racaCor.getSelectedItem());
    stmt.setString(20, assinatura.getText());
    stmt.setString(21, pis.getText());

    stmt.executeUpdate();
    stmt.close();

    String query2 = "insert into dadosprofissionais (cpf,numeroCasa,diretoria,departamento,divisao,matricula,inss,contaCorrente"
            + "agenciaBanco,numeroBanco,nivelEscolar,periodo,dataAdmissao,conselho)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    PreparedStatement stmt2 = con.prepareStatement(query2);        

    stmt2.setString(22, cpf.getText());
    stmt2.setString(23, numeroCasa.getText());
    stmt2.setString(24, diretoria.getText());
    stmt2.setString(25, departamento.getText());
    stmt2.setString(26, divisao.getText());
    stmt2.setString(27, matricula.getText());
    stmt2.setString(28, (String) inss.getSelectedItem());
    stmt2.setString(29, contaCorrente.getText());
    stmt2.setString(30, agenciaBanco.getText());
    stmt2.setString(31, numeroBanco.getText());
    stmt2.setString(32, (String) nivelEscolar.getSelectedItem());
    stmt2.setString(33, periodo.getText());
    stmt2.setString(34, ((JTextField)dataAdmissao.getDateEditor().getUiComponent()).getText());
    stmt2.setString(35, conselho.getText());

    stmt2.executeUpdate();
    stmt2.close();

    String query3 = "insert into endereco (cpf,numeroCasa,rua,ruaOrgao,bairro,bairroOrgao,cidade,cidadeOrgao,cep,ufFuncionario,ufOrgao) values(?,?,?,?,?,?,?,?,?,?,?)";

    PreparedStatement stmt3 = con.prepareStatement(query3);

    stmt3.setString(36, cpf.getText());
    stmt3.setString(37, numeroCasa.getText());
    stmt3.setString(38, rua.getText());
    stmt3.setString(39, ruaOrgao.getText());
    stmt3.setString(40, bairro.getText());
    stmt3.setString(41, bairroOrgao.getText());
    stmt3.setString(42, cidade.getText());
    stmt3.setString(43, cidadeOrgao.getText());
    stmt3.setString(44, cep.getText());
    stmt3.setString(45, (String) ufFuncionario.getSelectedItem());
    stmt3.setString(46, (String) ufOrgao.getSelectedItem());

    stmt3.executeUpdate();
    stmt3.close();

    String query4 = "insert into informacoesgerais (cpf,parenteEmpresa,grauParentesco,concordo,observacoes,dataFormulario)values(?,?,?,?,?,?)";

    PreparedStatement stmt4 = con.prepareStatement(query4);

    stmt4.setString(47, cpf.getText());
    stmt4.setString(48, (String) parenteEmpresa.getSelectedItem());
    stmt4.setString(49, (String) grauParentesco.getSelectedItem());
    stmt4.setString(50, (String) concordo.getSelectedItem());
    stmt4.setString(51, observacoes.getText());
    //stmt4.setModel(6, tabela.getDropMode());
    stmt.setString(52, ((JTextField)dataFormulario.getDateEditor().getUiComponent()).getText());

    stmt4.executeUpdate();
    stmt4.close();

    String query5 = "insert into telefone (cpf,foneFuncionario,celFuncionario,ramal,foneOrgao)values(?,?,?,?,?)";

    PreparedStatement stmt5 = con.prepareStatement(query5);

    stmt5.setString(53, cpf.getText());
    stmt5.setString(54, foneFuncionario.getText());
    stmt5.setString(55, celFuncionario.getText());
    stmt5.setString(56, ramal.getText());
    stmt5.setString(57, foneOrgao.getText());

    stmt5.executeUpdate();
    stmt5.close();

    String query6 = "insert into dependentes (cpf,conjugue,pai,mae,qtdDeFilhos,totalDependentes,profissaoConjugue"
            + "profissaoPai,profissaoMae,parenteEmpresa,nomeParenEmpresa,grauParentesco)values(?,?,?,?,?,?,?,?,?,?,?,?)";

    PreparedStatement stmt6 = con.prepareStatement(query6);

    stmt6.setString(58, cpf.getText());
    stmt6.setString(59, conjugue.getText());
    stmt6.setString(60, pai.getText());
    stmt6.setString(61, mae.getText());
    stmt6.setString(62, qtdDeFilhos.getText());
    stmt6.setString(63, totalDependentes.getText());
    stmt6.setString(64, profissaoConjugue.getText());
    stmt6.setString(65, profissaoPai.getText());
    stmt6.setString(66, profissaoMae.getText());
    stmt6.setString(67, (String) parenteEmpresa.getSelectedItem());
    stmt6.setString(68, nomeParenEmpresa.getText());
    stmt6.setString(69, (String) grauParentesco.getSelectedItem());


    stmt6.executeQuery(query6);
    stmt6.close();


    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Não foi possivel encontrar a classe...");

    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Ocorreu um erro no SQL...");   
    }             

    JOptionPane.showMessageDialog(null, "Dados e conexao extabelecida com sucesso...");
    cargoOcupado.requestFocus();
}                                      
  • what is error ? what happens when you execute this code?

  • 1- What is the error? 2- Separate each Insert execution into a separate method, so you isolate the problem.

  • The error is here catch (Sqlexception ex) { Joptionpane.showMessageDialog(null, "An error has occurred in SQL..."); }

  • These Inserts have to be inserted in a single command...

  • 1

    It would be nice in your error message to add the return of Sqlexception, so Voce has a good idea of what’s going on

  • 1

    Inside the Sqlexception catch puts ex.printStackTrace();, will print the error, then glue here for us to see.

Show 1 more comment
No answers

Browser other questions tagged

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