I am trying to list data from my database, more precisely from the Cpf column, but I can only bring a data

Asked

Viewed 34 times

0

List < VarUnica > list = new ArrayList < VarUnica > ();
    String queri = "select cpf from funcionario";
    VarUnica alt = new VarUnica();
    Statement ttpa;
    try {
        ttpa = Conexao.getConexao().createStatement();
        ResultSet tr = ttpa.executeQuery(queri);
        while (tr.next()) {
            alt.setRp(tr.getString("cpf"));

            list.add(alt);
        }
        for (VarUnica c: list) {
            System.out.println(alt.getRp());

        }

    } catch (SQLException ex) {
        Logger.getLogger(InserindoDados.class.getName()).log(Level.SEVERE, null, ex);
    }
}
  • Someone can get me where I’m going wrong,.

  • Can someone tell me where I’m going wrong,.

  • Try putting this: VarUnica alt = new VarUnica(); inside the While

  • you don’t need the other setters on alt? if you have alt.setRp() must have another like alt.setNome(), alt.setAlgumaOutraCoisa() ?

  • Can post the code where you open the connection to the bank?

  • First of all try to eliminate the possibility of errors, before the while see how many elements have the result set. I also think it should be because you are not instantiating the object within while, so it is not creating a new instance.

  • In column Cpf I have 3 records.

  • 1

    @Gleistonjosedesantana Tested what I said?

  • tested plus gave error...

  • @Gleistonjosedesantana What mistake?

  • Look in the database has 3 records,the result in the console is 3 records,more these 3 records is the same,ie are coming 3 equal data,what I want is to come the 3 different,got it.

  • @Gleistonjosedesantana I understood, but you did not formulate your question well and I think you are not doing right what I said.

  • apologies, could guide me in a clearer way.

  • People the idea of standardizing worked, more I had to put in Sys...(c.getRp);..... Thanks for the strength of the crowd.

  • You were only changing the object reference to each iteration. So you could only catch one object, the last one. A new object must be instantiated at each iteration.

Show 10 more comments

1 answer

4

You need to instantiate your object within the While because this way you will create a new object before putting it in a list. In your FOR you need to use the instance variable that was created in for.

for (VarUnica c: list) {
   //c represnta o objeto dentro dessa iteração
   System.out.println(c.getRp());
}

The whole Code would look like this:

List <VarUnica> list = new ArrayList <VarUnica> ();
    String queri = "select cpf from funcionario";
    Statement ttpa;
    try {
        ttpa = Conexao.getConexao().createStatement();
        ResultSet tr = ttpa.executeQuery(queri);
        while (tr.next()) {
            //Criando um novo objeto a cada iteração
            VarUnica alt = new VarUnica();
            alt.setRp(tr.getString("cpf"));
            list.add(alt);
        }
        for (VarUnica c: list) {
            System.out.println(c.getRp());
        }

    } catch (SQLException ex) {
        Logger.getLogger(InserindoDados.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Browser other questions tagged

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