Fill arrayList from Database in Java

1

I am trying to make a didactic software, to fill a Java arrayList with the data of a database table. I have managed to fill it so it seems, because the system.out.println shows me three objects that are the three objects that are in the database. Once I have filled the arrayList I want to show its contents but it returns the name of the columns equal to the number of objects there are. Could you teach me how to do it?

Deputy code:

Before starting to read, we would have to separate the code into classes and all that, but it was just for testing. since the next test will be to do this same but dumping the data in JTextField . Without more the code.

public class PruebaArrayListSerializable {
    protected static ArrayList<Object> badeDatos = new ArrayList<>();

    public static void main(String[] args) throws SQLException {
        MySQL mysql = new MySQL();
        ResultSet rs = null;
        //Object[][] obj = null;

        mysql.MySQLConnection("root", "1234", "pedidos");

        String sql = "SELECT * FROM clientes";

        rs = mysql.consultar(sql);

        ResultSetMetaData rsmd = null;

        rsmd = rs.getMetaData();
        int columnas = rsmd.getColumnCount();

            while(rs.next()){
                HashMap row = new HashMap();
                badeDatos.add(row);

                for(int i=1; i<=columnas;i++){
                    row.put(rsmd.getColumnName(i), rsmd.getColumnCount());

                }

            }  

           for(Object o : badeDatos) {
               System.out.println(o.toString());
           }

        }

Thanks in advance

I attach photo of the code and the output

    
asked by scorpions 12.06.2017 в 18:19
source

1 answer

1

What you need is a custom arrayList that receives the data, when I was working to use an arrayList, this link helped me, I hope that you also help: Link

(the link is not my responsibility) Edit: There is a part in the code shown in the link that performs the following:

animales.add(new Animal("aguila", R.drawable.aguila));

What you have to do to make it work with your fields, is to modify the "Animal" class by putting the fields you need, and at the time of inserting the class, you can do something like this:

for (int i = 0; i < columnas; i++) {
                badeDatos.add(new ClaseParaArrayList(Campo1,Campo2,Campo3));
            }
    
answered by 12.06.2017 в 18:24