I have to do a mysql query through a Java method. This is the sql I did to test if it works:
select * from servicos S, clientes C, motores M
where C.nome = "Claudio Fernando Pires"
group by ordemServico
Theoretically it should bring only the services whose client was "Claudio Fernando Pires" right? Correct me if you're wrong.
But it brings all the services, even if it is a golden client like "Claudio Fernando Pires", the same happens with any other parameter of the client or engine.
This is my method in Java:
public List<Servico> pesquisarServicos(String nomeCliente, String endereco, String marcaMotor, String modeloMotor) {
String sql = "SELECT * FROM clientes C, motores M, servicos S "
+ "WHERE C.nome LIKE ? AND C.endereco LIKE ? AND M.marcaMotor LIKE ? AND M.modeloMotor LIKE ?"
+ "GROUP BY ordemServico";
ResultSet listaResultados = null;
List<Servico> servicos = new ArrayList();
try {
PreparedStatement comando = BD.conection.prepareStatement(sql);
comando.setString(1, nomeCliente+"%");
comando.setString(2, "%"+endereco+"%");
comando.setString(3, marcaMotor+"%");
comando.setString(4, modeloMotor+"%");
listaResultados = comando.executeQuery();
Servico servico;
while (listaResultados.next()) {
servico = new Servico();
servico.setCliente(ControleCliente.buscarCliente(listaResultados.getString("CPF")));
servico.setDataServico(listaResultados.getString("dataServico"));
servico.setDescricaoServico(listaResultados.getString("observacao"));
servico.setFormaPagamento(Servico.getFormaPagamento(listaResultados.getInt("formaPagamento")));
servico.setMotor(ControleMotor.buscarMotor(listaResultados.getString("numMotor")));
servico.setOrdemServico(listaResultados.getString("ordemServico"));
servico.setTipoSevico(Servico.getTipoServico(listaResultados.getInt("tipoServico")));
servico.setValorServico(listaResultados.getDouble("valorServico"));
servicos.add(servico);
}
listaResultados.close();
comando.close();
System.out.println("Lista Resultante="+servicos.size());
} catch (SQLException excessaoSQL) {
excessaoSQL.printStackTrace();
}
return servicos;
}
It shows the same problem in my view written in Java.
Look at the result by filling in the name filter:
IfIchangethefilterName: Anysuggestionsonwhat'sgoingonandhowtomakethesearchworkforbothcases?
ThesearetherecordsintheServicestable:
IshouldbringtherecordsfortheclienttowhichIgavesomeattribute:
In this case I used an attribute from the Services table itself, but that's the idea.