1
I have to make 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 he should only bring the services whose client was "Claudio Fernando Pires" right? Correct me if I’m wrong.
Only that it brings all the services, even being gold customer as "Claudio Fernando Pires", the same happens with any other parameter of the customer or engine.
This is my Java method:
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;
}
Presents the same problem in my Java written view.
Look at the result by filling the name filter:
If I change the Name filter: Any suggestions of what’s going on and how to make the search work, for both cases?
These are the records in the Services table:
should bring the records relating to the client to whom I have informed some attribute:
in this case I used an attribute from the Services table itself, but this is the idea.
It didn’t work out the way you suggested. I will try to explain it in another way: I have a service that has as key the customer’s Cpf, that relates them, the same for the engine, being the serial number the key. What I need to do and for example: search a service stating the customer’s name Join worked in not duplicate the records, what had been happening. But it’s the way it is up there. Like, what I’m trying to do would be possible by going from Java to mysql?
– Julio Cesar da Silva Barros
Already tried to group by CPF?
– Giancarlo Abel Giulian
I have 4 record in the bank, it brought me 2 results both for: GROUP BY C.Cpf or GROUP BY S.clienteId. From what I understand, GROUP BY it restricts my search to 1 item of each key for GROUP BY that I informed right? In this case I can have more than one record for each client or engine, so restricting by ordem_servico would be enough for it not to duplicate my records as it happened previously.
– Julio Cesar da Silva Barros
These 4 records are all from one service, this? And you want to show only 1? And the grouping by service identifier, would be a good solution? And so add up the value of the service resulting in the total correctly.
– Giancarlo Abel Giulian
No, they’re all distinct, they can have identical values, but the ordem_servico makes them different. What I want is to bring a service informing a feature either of the engine or of the customer. Can you understand?
– Julio Cesar da Silva Barros
You could put an image of how the result should look and edit the question?
– Giancarlo Abel Giulian
Let’s go continue this discussion in chat.
– Giancarlo Abel Giulian
Guys, Giancarlo helped me a lot, now I’ve reached the point where when I inform some attribute to do the filtering, the same does not return me any item,?
– Julio Cesar da Silva Barros