Problems comparing objects from a list

0

I'm trying to compare an object from a list before inserting it into the database, but it returns the address of the object.

Follow the code.

DAO class 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 equal to that of the list, if it is not equal insert, 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 it returns a list of services

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 VerifyService method without it bringing me the address of the object in the incluirServico method.

    
asked by anonymous 10.05.2017 / 01:29

2 answers

0

Resolved.

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 boolean method it was easy to solve the problem.

    
14.05.2017 / 16:43
1

I do not know what your getNome_servico() returns but I imagine it's not a collection. Anyway, your verificaServico() method is returning a List , so you should iterate through that list and compare the value of an attribute (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 that you must change is to always start lowercase parameters. The parameter passing in methods incluirServico() and verificaServico() is starting with a capital letter.

    
10.05.2017 / 03:46