Skip insert a field with PreparedStatement

0

I want to ask a question that is difficult to formulate, to apologize in advance. When I am going to perform the PreparedStatement I check if the variable is NULL or it is empty with the textNoNULL () method, if the text is empty, as is the case with the IBAN variable, I want you to omit the action, but as in the statement you are waiting for a value "iba =?" I miss an error, I would like to be able to perform this action without error, that I sent a value that does not erase what the Database contains by an empty field or a NULL.

public void actualizarCuentaBancaria(){

      private String iban = "";
      private String entidad = 9012;
      private String sucursal= 0415;

            try {                
                    String sentencia = "UPDATE cuenta_bancaria SET iban= ?, entidad= ?, sucursal= ? WHERE id_persona = ?";

                    PreparedStatement pst = getConnection().prepareStatement(sentencia);                    


                    if(textoNoNULL(iban)) pst.setString(1, iban); else //AQUI
                    if(textoNoNULL(entidad)) pst.setString(2, entidad);
                    if(textoNoNULL(sucursal)) pst.setString(3, sucursal);

                    pst.setInt(6, idPersona);

                    pst.executeUpdate();
                    pst.close();
                } catch (SQLException ex) {

                } 
}

private boolean textoNoNULL(String palabra){
        if(palabra == null || palabra.length() == 0)
            return false;
        else 
            return true;    
}
    
asked by Arkhan6 02.01.2017 в 10:56
source

2 answers

2

If only he was going, it could be null, so it would be enough:

if(textoNoNULL(iban)
       sentencia = "UPDATE cuenta_bancaria SET iban= ?, entidad= ?, sucursal= ? WHERE id_persona = ?";
else
       sentencia = "UPDATE cuenta_bancaria SET entidad= ?, sucursal= ? WHERE id_persona = ?";
                    PreparedStatement pst = getConnection().prepareStatement(sentencia);                    

                    int i=1;
                    if(textoNoNULL(iban)) {pst.setString(i, iban); i++;} //AQUI
                    if(textoNoNULL(entidad)) pst.setString(i, entidad); i++;
                    if(textoNoNULL(sucursal)) pst.setString(i, sucursal); i++;
                    pst.setInt(i, idPersona);

                    pst.executeUpdate();
                    pst.close();

If the other fields can also be null you should build the sentence concatenated texts depending on whether they are null or not, a solution would be something like this:

    sentencia="UPDATE cuenta_bancaria SET ";
    boolean comapendiente=false;
    if(textoNoNULL(iban) 
         {sentencia+="iban=? ";
          comapendiente=true;
         }
    if(textoNoNULL(entidad) 
          {if(comapendiente) sentencia+=",";
         sentencia+="entidad=? ";
          comapendiente=true;
         }
    if(textoNoNULL(sucursal) 
          {if(comapendiente) sentencia+=",";
         sentencia+="sucursal=? ";
         }
    sentencia+="WHERE id_persona = ?";
 int i=1;
                    if(textoNoNULL(iban)) {pst.setString(i, iban); i++;} //AQUI
                    if(textoNoNULL(entidad)) pst.setString(i, entidad); i++;
                    if(textoNoNULL(sucursal)) pst.setString(i, sucursal); i++;
                    pst.setInt(i, idPersona);

                    pst.executeUpdate();
                    pst.close();
    
answered by 02.01.2017 / 11:57
source
-1

I have improved the solution, I have deleted the variable "compendium" by a subString that deletes the last character of the String, that is, eliminates the "comma".

public void actualizarCuentaBancaria(){
        try {  
                String sentencia = "UPDATE cuenta_bancaria SET ";

                if(textoNoNULL(iban)){
                    sentencia += " iban = ?,";
                }
                if(textoNoNULL(entidad)){
                    sentencia += " entidad= ?,";
                }
                if(textoNoNULL(sucursal)){
                    sentencia += " sucursal= ?,";
                }

                //Elimina la "coma" 
                sentencia = sentencia.substring(0, sentencia.length()-1); 

                sentencia +=" WHERE id_persona = ? ";

                int i = 1;

                PreparedStatement pst = getConnection().prepareStatement(sentencia);                                 

                if(textoNoNULL(iban)){ pst.setString(i, iban);  i++; }
                if(textoNoNULL(entidad)){ pst.setString(i, entidad);  i++; }
                if(textoNoNULL(sucursal)){ pst.setString(i, sucursal);   i++; }              

                pst.setInt(i, idPersona);

                pst.executeUpdate();
                pst.close();
            } catch (SQLException ex) {

            }
    }

private boolean textoNoNULL(String palabra){
        if(palabra == null || palabra.length() == 0)
            return false;
        else 
            return true;
}
    
answered by 02.01.2017 в 13:40