Incompatible types boolean can not be converted to String

1

What happens is that I'm making a library application and when I click on the Save button, I get this error:

  

Incompatible types boolean can not be converted to String

here is the code:

Save button:

Connection con=null;
        try{
            con=getConnection();
            ps=con.prepareStatement("INSERT INTO clientes (rutClientes, Nombre, ApellidoPaterno, ApellidoMaterno, Direcciones, Telefonos, Correos, FechaNac) VALUES(?,?,?,?,?,?,?,?)");
            if(ps.setString(1, txtRut.getText().equals(""))){ (AQUI ME SALE EL ERROR)
                JOptionPane.showMessageDialog(null, "Rellene el campo de RUT");
            }
            if(txtNombre.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo de NOMBRE");
            }
            if(txtApellidoPaterno.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo de APELLIDO PATERNO");
            }
            if(txtApellidoMaterno.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo de APELLIDO MATERNO");
            }
            if(txtDireccion.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo DIRECCION");
            }
            if(txtTelefono.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo TELEFONO");
            }
            if(txtCorreo.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo CORREO");
            }
            if(txtFechaNac.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo de FECHA DE NACIMIENTO");
            }
            int res=ps.executeUpdate();
            if(res>0){
                JOptionPane.showMessageDialog(null, "Se ejecuto correctamente");
                limpiarCajas();
            }else{
                JOptionPane.showMessageDialog(null, "Error al ejecutar la conexion");
                limpiarCajas();
            }
        }catch(Exception e){
            System.err.println(e);
        }
    
asked by nicolasyo1WWE 07.07.2018 в 00:56
source

1 answer

2

The problem is that this gets a boolean since you are making a comparison:

txtRut.getText().equals("")

But the method setString (int parameterIndex, String x) only allows you to receive a int and a String and not a int and% booleano .

You must change to only get the text value by txtRut.getText() , not using the equals() method.

ps.setString(1, txtRut.getText());

Now another problem, setString (int parameterIndex, String x) does not return any value, so it is incorrect to define it within an if

 if(ps.setString(1, txtRut.getText())){ 
  ...

You must call this method and not use it within if() :

 try{
            con=getConnection();
            ps=con.prepareStatement("INSERT INTO clientes (rutClientes, Nombre, ApellidoPaterno, ApellidoMaterno, Direcciones, Telefonos, Correos, FechaNac) VALUES(?,?,?,?,?,?,?,?)");
            ps.setString(1, txtRut.getText());

            if(txtRut.getText().equals("")){ 
                JOptionPane.showMessageDialog(null, "Rellene el campo de RUT");
            }
            if(txtNombre.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Rellene el campo de NOMBRE");
            }
            ...
            ...
    
answered by 07.07.2018 / 01:00
source