0
I’m having a problem printing Mysql data in Eclipse. When I put to print the list of registered data it is printing 3 times each information.
I know where the problem is, but I can’t see the solution. My database has a schema and two tables (cliente
and endereco
). What happens is that when I enter the information of 3 people, for example, Eclipse prints 3 times each information because the rs.next()
is taking from both tables.
Look at:
public void listarClientes() {
String sql = "SELECT * FROM locadora.cliente, locadora.endereco";
try {
PreparedStatement ps = jdbc.getConexao().prepareStatement(sql); ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println("Nome: " + rs.getString("nome") + " - Código: " + rs.getString("codigo") + " - Bom pagador: " + rs.getBoolean("seBomPagador") + " - Telefone: " + rs.getString("telefone"));
System.out.println("Tipo de endereço: " + rs.getString("tipo_endereco") + " - Logradouro: " + rs.getString("logradouro") + " - Bairro: " + rs.getString("bairro") + " - Cidade: " + rs.getString("cidade") + " - UF: " + rs.getString("uf") + " - Cep: " + rs.getString("cep"));
System.out.println();
}
System.out.println(); rs.close();
} catch (Exception e) { // TODO: handle exception
System.out.println("Erro na listagem dos clientes!");
}
Your problem should be in sql. Try to do more or less this way:
SELECT * FROM cliente as c INNER JOIN endereco as e ON (c.idEndereco = e.id)
. How the two tables should have a relationship, the tablecliente
must contain an id of the tableendereco
, this way you can bring the data of the address of your respective client.– Thiago Magalhães
Thanks Thiago, I’ll see if I can!
– Allan Jaqueira
Recommended reading: https://answall.com/q/172909/132
– Victor Stafusa