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 insideverificarServico(...)
? Then you could doverificarServico(...)
return aboolean
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).– Douglas
Post the method code
getNome_servico()
, please.– igventurelli
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.
– junio