Search code examples
javamysqlsqlarraylistresultset

How save resultset in an ArrayList saving different row in the same cell?


pstpr = con.prepareStatement("select id_prodotto from prodotto_autore where id_autore='" + id.getInt(1) + "';");
            prodotti = pstpr.executeQuery();
            while (prodotti.next()) {
                pstcoa = con.prepareStatement("SELECT id_autore FROM prodotto_autore  WHERE id_prodotto='" + prodotti.getString(1) + "';");
                coautori = pstcoa.executeQuery();
                List<String> row =null;
                while (coautori.next()) {
                     row = new ArrayList<>(); // new list per row
                     row.add(coautori.getString(1));

                    //prodott.add(coalizione.getString(2));
                }
                coaliz.add(row);
            }

where from the results of the first resultset i can compute the results of the second resultset. Now i want to save this results in an arraylist but in the same cell. I mean what i have is

[[2], [79], [2], [2], [2], [2], [2]]

but what i need is instead

[[2, 79], [2], [2], [2, 2, 2]]

How can i do? thank you


Solution

  •     pstpr = con.prepareStatement("select id_prodotto from prodotto_autore where id_autore='" + id.getInt(1) + "';");
            prodotti = pstpr.executeQuery();
            while (prodotti.next()) {
                pstcoa = con.prepareStatement("SELECT id_autore FROM prodotto_autore  WHERE id_prodotto='" + prodotti.getString(1) + "';");
                coautori = pstcoa.executeQuery();
                List<String> row =null;
                row = new ArrayList<>();
                while (coautori.next()) {
                      // new list per row
                     row.add(coautori.getString(1));
    
                    //prodott.add(coalizione.getString(2));
                }
                coaliz.add(row);
            }
    

    You are trying to create a new array list inside the second result set. Instead create it outside the second result set and within the first result set.