Problems to list BD MYSQL in eclipse

Asked

Viewed 50 times

0

I’m having a problem in my Java code at the time of listing the Mysql BD in Eclipse.

It turns out that when I put it to list the data, it’s listing the information 3 times but when I do the SELECT * FROM in Mysql the data is being listed correctly. Can anyone help me? Below is the method to list the data.

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();


    } catch (Exception e) {
        // TODO: handle exception

        System.out.println("Erro na listagem dos clientes!");
    }

}
  • Without knowing the data of the tables, the output of the program and what should actually return, it is difficult to give an accurate answer (if you want - and I suggest you do - you can [Edit] the question, so that it is a [mcve]). Only with the information that is in the question so far, I would guess that instead of FROM locadora.cliente, locadora.endereco, you should do a JOIN with the client and address tables. But it’s just a kick...

  • The problem in your case is not the listing, but the query, which is not having a relationship between the tables.

  • How do I fix it?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.