title: JDBC Select all records from table, returns only 1
body: <p>Hi I want to select all records from Garaz table and add it to ArrayList (now it gives me only one Garaz from Garaz table), then return the ArrayList. Also if any Garaz contains cars (Auta) i want to list them too - for a particular Garaz (Garaz, Auta - names in Polish).  </p>
  
  <pre><code>public class connection {
  
  private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  private static final String DB_CONNECTION = "jdbc:mysql://localhost/garaz";
  private static final String DB_USER = "root";
  private static final String DB_PASSWORD = "";
  static ArrayList &lt;Garaz&gt; lista = new ArrayList &lt;Garaz&gt;();
  static ArrayList &lt;Auto&gt; lista2 = new ArrayList &lt;Auto&gt;();
  
  public static ArrayList &lt;Garaz&gt; selectRecords() throws SQLException {
  
      Connection dbConnection = null;
      Statement statement = null;
  
      String selectTableSQL = "SELECT * from GARAZ";
  
  
              try {
          dbConnection = getDBConnection();
          statement = dbConnection.createStatement();
  
          System.out.println(selectTableSQL);
  
          // execute select SQL stetement
          ResultSet rs = statement.executeQuery(selectTableSQL);
  
          while (rs.next()) {
                          int g_id = rs.getInt("G_ID");
          String nazwa = rs.getString("NAZWA");
          String adres = rs.getString("ADRES");
                          int lmiejsc = rs.getInt("LICZBAMIEJSC");
          int lpoz = rs.getInt("LICZBAPOZIOMOW");
                          boolean czynny = rs.getBoolean("CZYNNY");
  
              ArrayList lista2 = new ArrayList &lt;Auto&gt;();
  
  
          int a_id = rs.getInt("A_Id");
              String model = rs.getString("Model");
              String kolor = rs.getString("Kolor");
              int ildrzwi = rs.getInt("IloscDrzwi");
              String rejestracja = rs.getString("Rejestracja");
  
              Auto d = new Auto(a_id, model, kolor, ildrzwi, rejestracja);
              if (a_id !=0){
              lista2.add(d);
              }
              Garaz f = new Garaz(g_id, nazwa, lista2, adres, lmiejsc, lpoz, czynny);
              lista.add(f);
                     // return lista;
                      }
  
      } catch (SQLException e) {
  
          System.out.println(e.getMessage());
  
      } finally {
  
          if (statement != null) {
              statement.close();
          }
  
          if (dbConnection != null) {
              dbConnection.close();
          }
  
      }
      return lista;
  
  }
  </code></pre>