The values of an arraylist are repeated

0

Well the problem is as follows; I'm doing a program that has a database and I want to return a query in a list made with an access class (getter and setter) ... but when I see the list that returns the function returns me the amount of queries but they all have the same information as the last query.

This is the code:

try{ 
        if (reporte=='g'){

            try {
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(aSQL);

            while (rs.next()) {
             int i=0;

              dtsGe.setCodigo(rs.getString("codigo"));
              dtsGe.setDescripcion(rs.getString("descripcion"));
              dtsGe.setCategoria(rs.getString("categoria"));

              dtsGe.setCod_interno(rs.getString("codigo_interno"));
              dtsGe.setNota(rs.getString("nota"));

              dtsGe.setLote(rs.getInt("lote"));
              dtsGe.setF_vencimiento(rs.getDate("f_vencimiento")); 
              dtsGe.setF_entrada(rs.getDate("f_entrada"));
              dtsGe.setProveedor(rs.getString("proveedor"));
              dtsGe.setDoc_entrada(rs.getString("doc_entrada"));
              dtsGe.setF_entrada(rs.getDate("f_docentrada"));
              dtsGe.setPrec_compra(rs.getDouble("prec_compra"));

              dtsGe.setF_salida(rs.getDate("f_salida"));
              dtsGe.setCliente(rs.getString("cliente"));
              dtsGe.setDoc_salida(rs.getString("doc_salida"));
              dtsGe.setF_salida(rs.getDate("f_docsalida"));
              dtsGe.setPrec_venta(rs.getDouble("prec_venta"));

              dtsGe.setNom_envio(rs.getString("nom_envio"));
              dtsGe.setCod_guia(rs.getString("cod_guia"));
              dtsGe.setF_entrega(rs.getDate("f_entrega"));
              dtsGe.setF_aprobado(rs.getDate("f_aprobado"));
              dtsGe.setF_devolucion(rs.getDate("f_devolucion"));
              dtsGe.setF_garantia(rs.getDate("f_garantia"));
              dtsGe.setMotivo(rs.getString("motivo"));
              dtsGe.setNota2(rs.getString("nota2"));

              lista.add(i,dtsGe);
              System.out.println(lista.get(i).getCod_interno());
              i++;
            }

            System.out.println(lista.get(10).getCod_interno());
            return lista;

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
        }

In itself this is the interesting part since the same problem is repeated in similar methods the first Print shows me all the unique codes of the products, there the list is fine, but in the second Print to want to see the code of any product (in the example 10) I get only the last one ...

I do not know what I'm doing wrong: C, I appreciate your help.

    
asked by Miguel Piña 19.05.2016 в 22:40
source

1 answer

1

inside the while creates a new intance of the dtsGe object

while(rs.next()){
  miClase dtsGe = new miClase();
  //aqui seteas los valores a tu clase

 //al final agregas ese objeto a tu lista
 lista.add(dtsGe);


}
    
answered by 10.04.2017 в 21:08