Problems comparing objects coming from a list

Asked

Viewed 70 times

0

I’m trying to compare an object coming from a list before entering it in the database only it returns the object address.

Follows the code.

Class DAO method

public List<ServicoVO> verificaNome(ServicoVO servico) throws SQLException{

        String sql = ("SELECT nome_servico FROM servico  where nome_servico like '%" + servico.getNome_servico() + "%'");
        List<ServicoVO> servicos = new ArrayList<ServicoVO>();
        PreparedStatement stmt = con.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        while(rs.next()){
            servico.setNome_servico(rs.getString("nome_servico"));
            servicos.add(servico);
        }
        return servicos;

    }

Methods of class BO

Here I compare if the name is the same as the list, if it is not the same you enter, if it is not inserted.

public void incluirServico(ServicoVO ServicoVO) throws SQLException {


        ServicoDAO dao = new ServicoDAO();
        if(verificaServico(ServicoVO).equals(ServicoVO.getNome_servico())){
            System.out.println("Valores iguais , item não cadastrado");
        }else{
            dao.incluirServico(ServicoVO);
        }

    }

Here returns a service list

public List<ServicoVO> verificaServico(ServicoVO Servico) throws SQLException {

        List<ServicoVO> servico = new ServicoDAO().verificaNome(Servico);

        return servico;
    }

Class Servicovo

public String getNome_servico() {
        return nome_servico;
    }
    public void setNome_servico(String nome_servico) {
        this.nome_servico = nome_servico;
    }

How could I compare the result of the Rvico verified method without bringing me the address of the object in the method incluirServico.

  • "if(verificaServico(ServicoVO).equals(ServicoVO.getNome_servico())" is a check, she wouldn’t look better inside verificarServico(...)? Then you could do verificarServico(...) return a boolean that I think makes much more sense for a method that checks something (or even, the verification method could launch an Exception depending on the case).

  • Post the method code getNome_servico(), please.

  • Obg by the answer. By the colleague’s code below I can verify , but there is another problem when the services are not equal he does not enter the for.. I don’t know what’s going on.I posted the code you asked me to t.

2 answers

1

I don’t know what your getNome_servico() returns but I guess it’s not a collection. Anyway, your method verificaServico() is returning a List, this way you should iterate (go through) this list and compare the value of an attribute (I imagine the name), something like this:

ServicoDAO dao = new ServicoDAO();

for (ServicoVO servico : verificaServico(ServicoVO)) 
{
   if(servico.getNome_servico().equals(ServicoVO.getNome_servico())){
       System.out.println("Valores iguais , item não cadastrado");
   }else{
       dao.incluirServico(ServicoVO);
   }
}

Another important point you should change is to start parameters always in lower case. Passing parameter in methods incluirServico() and verificaServico() is starting in capital letters.

  • Thank you very much for your reply. Now he’s comparing , but when the values are different he doesn’t even enter the for and I can’t insert.I did some tests by putting sysout before the if and doesn’t even show on the console.

  • The code block inside the for will be reached only if the collection returned in the method verificaServico() have at least one value. What is probably happening in your case is that the returned collection has no value. Since you are debugging using sysout, put one before the block of the for to verify the return of the method.

  • Obg by the help I solved a while ago only that I forgot to put with the rush .

0


Solved.

public boolean incluirServico(ServicoVO servicoVO) throws SQLException {


    ServicoDAO dao = new ServicoDAO();

        System.out.println(verificaServico(servicoVO));
        if(verificaServico(servicoVO)){
        System.out.println("Inserido");
        dao.incluirServico(servicoVO);
        return true;
        }
        return false;


}

public boolean verificaServico(ServicoVO servicoVO) throws SQLException {

    List<ServicoVO> servico = new ServicoDAO().verificaNome(servicoVO);
    for (ServicoVO ser : servico) {
     if(ser.getNome_servico().equals(servicoVO.getNome_servico()));
    return false;
    }
    return true;

}

With the booleam method it is easy to solve the problem.

Browser other questions tagged

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