Problem when entering data into mysql database by java

Asked

Viewed 603 times

1

My java code is working, but when adding data to an SQL table, the fields are being populated as NULL, even though added values.

Part of the code into which the insertion occurs:

public void salvarCliente() throws ClassNotFoundException, SQLException {

    Connection conexao = new FabricaDeConexao().retornaConexao();


    String query = "INSERT INTO cli(nome,endereco,cpf) values (?,?,?)";

    PreparedStatement stmt = conexao.prepareStatement(query);

    stmt.setString(1, textFieldNome.getText());
    stmt.setString(2, textFieldEndereco.getText());
    stmt.setString(3, textFieldCpf.getText());


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

}

NOTE: The code was working correctly. The error started when I added a data removal method. I used the command:

String query = "delete from cli where id=(select max(id))";

To delete the last record inserted in the table (removal done by ID).

Instead of erasing the last record, it erased all data from the table and from then on started inserting the values as NULL.

  • This behavior is as strange as it is, are sure that the textFieldare populated, and are not null? And could provide us with the table format?

  • Yes, I enter all the data correctly, as created in the database, except the ID, because I put to increment automatically. Follow the table created. create table cli( id int not null auto_increment, name varchar(50), address varchar(50), Cpf varchar(43), Primary key(id) );

  • Friend, sorry for the delay of the answer, I did not have a good weekend. Your query, it really works without informing the FROM in subselect? Maybe that’s what’s going wrong.

  • Thanks for the return, I was able to solve the problem with the help of my professor from college, the mistake was the following, in this my class Provider, which has the implementation of the methods, I put to extend my registration classClient( extends cadastral)where it has the visual part, and when it gave 'stmt.setString' with the textfield, it always took the values of the other class, which are null. To solve, in the register class methodClient I passed the textField by parameter, and passed until I arrived in the class Provider, then I only set in my variables.

  • If I have to, I can release the code here to anyone with similar doubts.

  • Answer your question with the solution and vote for it as the best answer, so closes the question :)

Show 1 more comment

1 answer

0


Thanks for the return, I was able to solve the problem with the help of my professor from college, the mistake was the following, in this my class Provider, which has the implementation of the methods, I put to extend my registration classClient( extends cadastral)where it has the visual part, and when it gave stmt.setString with the textfield, it always took the values of the other class, which are null. To solve, in the register class methodClient I passed the textField by parameter, and passed until I arrived in the class Provider, then I only set in my variables

Browser other questions tagged

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