Save button that serves to update

0

I appeal to you since I have a question and can not think of anything to solve it. I comment I'm doing a CRUD in Java with the BD Mysql. I have done almost all the buttons, I just need to update. It turns out that I want the Save button to also update the fields. That is, when I select a field in the table, I load it in the JFields and then modify them and save them with the same button without creating a new one.

I have the following example.

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                           
    terceros objTercero = new terceros();
    String nombres = txtNombres.getText();
    String apellidos = txtApellidos.getText();
    int telefono = Integer.parseInt(txtTelefono.getText());
    int id = Integer.parseInt(txtId.getText());

    if(id == 0){
        boolean resultado = objTercero.insertartercero(nombres,apellidos,telefono);
        if(resultado == true){
            JOptionPane.showMessageDialog(null, "Se inserto un nuevo registro");
            cargarTabla();
        }else{
            JOptionPane.showMessageDialog(null, "Error al insertar");
        }
    }else{
        boolean resultado = objTercero.actualizartercero(id, nombres,apellidos,telefono);
        if(resultado == true){
            JOptionPane.showMessageDialog(null, "Se inserto un nuevo registro");
            cargarTabla();
        }else{
            JOptionPane.showMessageDialog(null, "Error al insertar");
        }    

    }

As you can see there is a condition in which the id = 0, in that table the ID is not supplied since it is autoincrementable. But in the table that I'm doing the ID is the identification of the user so I can not leave it at 0. I would like to please help me think of a condition similar to the previous one.

Thank you very much!

Thanks for your reply.

I've been thinking about something and it seems a bit like what you say. The idea that I have is that, before inserting the data, to put a sentence that evaluates to me if the id exists in the BD, if it is so that I update the record, otherwise I keep the record. But the problem is that when I do the query is to say. Axis: String query="Select fron ..."; After that I want a variable of Boolean type to store the result. If the query was executed then send me a Boolean value and then program that value into a conditional. If it is true (that is, the data is in the DB) I update it and if not, it saves the value. I could not do the Boolean type. Could you help me please ?

    
asked by Permomo 04.11.2018 в 22:23
source

1 answer

0

When you are going to edit something, first you have to know what it is, therefore you should have accessed the database first to get what you want to edit and within that data comes the ID , in the Save method you can ask for the ID of the object and if it is greater than 0 then it is already a value that exists in the database and within that condition you save it. < br>

if(id > 0){
    // codigo de actualizar
}else{
   // codigo de guardar
}

I hope you understand what I mean.

To know if the query executed has values or not, you can use this

if(query.first()){
  //consulta vacia
}else{
  //consulta con algun valor
}
    
answered by 06.11.2018 / 16:28
source