title: How to load a List of Objects from a Database using jdbc
body: <p>I'm trying to learn my ways around saving and loading data to/from Databases and i came across a problem. At the moment i have a class called <code>Component</code> that has the following:</p>
  
  <pre><code>public class Componente {
  
      private int code;
      private String description;
      private double price;
      private int quantity;
      private List&lt;Componente&gt; previous;
      private List&lt;Componente&gt; incompatible;
  </code></pre>
  
  <p>I also have a class called <code>Storage</code> wich has the following:</p>
  
  <pre><code>public class Storage {
  
      private List&lt;Component&gt; stock;
  </code></pre>
  
  <p>There are other classes where i was able to create the save and load methods becase they only had simple variables like String's or Integers, but for these ones i'm completely lost because not only are there Lists to be saved/returned but there's also recursive Lists. What i have so far for the Components is:</p>
  
  <pre><code>public void save(Component c)throws SQLException{
      Connection con = null;
  
      con = Connect.connect();
      PreparedStatement st = con.prepareStatement("INSERT INTO component 
      VALUES(?,?,?,?,?,?)");
  
      st.setInt(1, c.getCode());
      st.setString(2, c.getDescription());
      st.setDouble(2, c.getPrice());
      st.setInt(2, c.getQuantity());
      //Missing the last 2 variables
      st.executeUpdate();
  
      con.close();
  }
  
  public Component load(Object key) throws SQLException {
      Component c = null;
      Connection con = Connect.connect();
  
      PreparedStatement ps = con.prepareStatement("select * from component 
      where code = ?");
      ps.setInt(1, code);
      ResultSet rs = ps.executeQuery();
  
      if(rs.next()){
          c = new 
  
         Component(rs.getInt("code"),rs.getString("description"),
         rs.getDouble("price"),rs.getInt("quantity"));
      }
      con.close();
  
      return c;
  }
  </code></pre>
  
  <p>There are exceptions to be handled, but in that part i think i'm good. Also if i'm able to do it for Components i'll probably be able to set it up for Storage too, so for now i think this question is already large enough.</p>