JDBC - does not connect to Mysql database

4

In java I created a project where I use the drive mysql-connector-java-5.1.40-bin.jar In this project I created a class with a form to register course and in mysql I created a database called sistema with a table called curso with the fields:

ThenIcreatedthefollowingcodeinsidetheIncludebuttonmethodtoregisterthecourse:

@OverridepublicvoidactionPerformed(ActionEvente){if(e.getSource()==BIncluir){//procuraaclassecom.mysql.jdbc.Drivertry{Class.forName("com.mysql.jdbc.Driver");

                    //cria uma variável:
                    Connection con;

                    //cria uma conexão com o banco de dados
                    con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/sistema", "root", "");

                    //minha query
                    String query = "INSERT INTO curso (cod_curso, nm_curso, tipo_curso, carga_h, cod_instituto) VALUES(?,?,?)";

                    //cria o camando
                    PreparedStatement stmt = con.prepareStatement(query);

                    //seta os valores na string de inserção
                    stmt.setString(1, CodCurso.getText());
                    stmt.setString(2, Txt1.getText());
                    stmt.setString(3, Txt2.getText());
                    stmt.setString(4, CargaHoraria.getText());
                    stmt.setString(5, CodInstituto.getText());

                    //executa o comando no banco de dados 
                    stmt.executeUpdate();

                    //fecha o comando e a conexão com o banco de dados
                    stmt.close();
                    con.close();

                } catch (ClassNotFoundException ex) { //tratador de erro do comando Class.forName
                    System.out.println("Não foi possível encontrar a classe");
                } catch (SQLException ex) { //tratador de erro do comando con = DriverManager.getConnection
                    System.out.println("Não foi possível conectar ao banco de dados");
                }

I made connection tests before including the query and it was connecting, but stopped connecting when it included this part of the code that creates and executes the query:

//minha query
                String query = "INSERT INTO curso (cod_curso, nm_curso, tipo_curso, carga_h, cod_instituto) VALUES(?,?,?)";

                //cria o camando
                PreparedStatement stmt = con.prepareStatement(query);

                //seta os valores na string de inserção
                stmt.setString(1, CodCurso.getText());
                stmt.setString(2, Txt1.getText());
                stmt.setString(3, Txt2.getText());
                stmt.setString(4, CargaHoraria.getText());
                stmt.setString(5, CodInstituto.getText());

                //executa o comando no banco de dados 
                stmt.executeUpdate();

                //fecha o comando e a conexão com o banco de dados
                stmt.close();
                con.close();

When I hit the Include button it shows the meshes I set in try catch: "Não foi possível conectar ao banco de dados" Please, anyone who knows helps me !!!! : 0

    
asked by anonymous 02.11.2016 / 17:49

1 answer

4

Your query has 3 parameters and you are trying to set 5 parameters. Ideally, in error handling you use the message returned by the exception, so it will not hide the actual error.

System.out.println(ex.getMessage());
    
02.11.2016 / 18:03