incompatible types Boolean cannot be converted to Usuarios

Asked

Viewed 272 times

0

I am creating a function to insert new users in the database, but present this error: "incompatible types: Boolean cannot be converted to Usuarios". Does anyone know a way to solve this case:

public String InsertUserCpf(@PathParam("nome")String nome,
        @QueryParam("email") String email,
        @QueryParam("senha") String senha,
        @QueryParam("dtnasc") Date dtnasc,
        @QueryParam("fone") String fone,
        @QueryParam("oab") String oab,
        @QueryParam("cep") String cep,
        @QueryParam("cpf") String cpf)
{
    Usuarios u = new Usuarios();
    u.setNome(nome);
    u.setEmail(email);
    u.setSenha(senha);
    u.setDtnasc(dtnasc);
    u.setFone(fone);
    u.setOab(oab);
    u.setCep(cep);
    u.setCpf(cpf);

    UsuarioDAO dao = new UsuarioDAO();
    u = dao.inserir(u);

    Gson g = new Gson();
    return g.toJson(u);
}

Usuariodao.java:

public boolean inserir(Usuarios usuario)
    {
        String sql = "INSERT INTO usuarios(nome,email,senha,dtnasc,fone,oab,cep,cpf) VALUES(?,?,?,?,?,?,?,?)";
        Boolean retorno = false;
        PreparedStatement pst = Conexao.getPreparedStatement(sql);
        try {
            pst.setString(1, usuario.getNome());
            pst.setString(2, usuario.getEmail());
            pst.setString(3, usuario.getSenha());
            pst.setDate(4, usuario.getDtnasc());
            pst.setString(5, usuario.getFone());
            pst.setString(6, usuario.getOab());
            pst.setString(7, usuario.getCep());
            pst.setString(8, usuario.getCpf());

            if(pst.executeUpdate()>0)
            {
                retorno = true;
            }



        } catch (SQLException ex) {
            Logger.getLogger(UsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            retorno = false;
        }

        return retorno;

    }
  • Where does the error occur? Which line?

  • this command which shows the error: u = dao.insert(u);

  • Return boolean to signal whether error or not is a bad programming practice. This hides errors, their causes and makes it difficult to recover. It was precisely so that programmers would not do this, that the exceptions were invented, so use them.

  • From what you’re saying, it seems to be the case mark an answer as accepted. Here we do not write "solved" in the question. If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems

1 answer

2


Note that you declare a Usuario u and then does receive the method inserir() that returns a Boolean and not a user. That is, they are different types. Note:

Usuarios u = new Usuarios(); // aqui você instancia
u.setNome(nome);
u.setEmail(email);
u.setSenha(senha);
u.setDtnasc(dtnasc);
u.setFone(fone);
u.setOab(oab);
u.setCep(cep);
u.setCpf(cpf);

UsuarioDAO dao = new UsuarioDAO();
u = dao.inserir(u); // aqui tenta receber em "u" o retorno de dao.inserir() 
                    //que no caso é um boolean (true ou false) e não um Usuario
Gson g = new Gson();
return g.toJson(u);

From what I understand, the insert method returns a boolean which refers to the successful insertion. Soon its logic would look something like this:

boolean sucesso = false;
sucesso = dao.inserir(u);

if (sucesso) { // sucesso ao inserir
    Gson g = new Gson();
    return g.toJson(u);
    //...
}

However, as @Victor Stafusa mentioned, use boolean to return errors is bad programming practice, instead use Exceptions

  • 2

    Remembering that returning boolean to indicate success or error is a bad programming practice.

  • 1

    Thanks @Zulian, you solved the problem and I’ll try to include Exceptions now. Thanks for the tip

Browser other questions tagged

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