title: Assign ResultSet to variables in class
body: <p>I have a following function:</p>
  
  <pre><code>public Map&lt;Integer, Product&gt; fetchAllProducts() {
  
    Map&lt;Integer, Product&gt; pArr = new HashMap();
    try {
       Statement st = conn.createStatement();
       ResultSet rs = st.executeQuery("SELECT id, intro, content, price FROM Product");
       while (rs.next()) {
         pArr.put(rs.getInt("id"), new Product(rs));
       }
       st.close();
    } catch (SQLException ex) {
     //...
    }
    return pArr;
  }
  </code></pre>
  
  <p>which gets all the rows from mySQL table <code>Product</code> and for each row creates a new <code>Product</code> class. Product constructor:</p>
  
  <pre><code> public Product(ResultSet rs) {
      try {
         price = rs.getInt("price");
         content = rs.getString("content");
         intro = rs.getString("intro");
      } catch (SQLException ex) {
         //...
      }
     }
  </code></pre>
  
  <p>My question is: is there a better way to assign the result columns to variables in <code>Product</code>? The code <code>price = rs.getInt("price");</code> and so on seems redundant, doesn't it? Perfect would be, if I change the query statement to <code>SELECT intro, content, tax, delivery FROM ...</code> the constructuor would automaticlly assign it to the appropriate variables (i.e. intro, content, tax, delivery) in the constructor. Can this be done in Java or am I just dreaming?</p>