title: Java - Is storing the result of select statement in arraylist is good or bad approach?
body: <p>I am just starting to learn with SQL server 2008 in java. So I was wondering if using this approach is a good one or not? I implemented a generic arraylist method which will get the values from the database.</p>
  
  <pre><code>public ArrayList&lt;String&gt; get(){
      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      ArrayList&lt;String&gt; a = new ArrayList&lt;String&gt;();
      try {
  
          // Register JDBC driver
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          conn = DriverManager.getConnection("jdbc:sqlserver://localhost; database = Sample; Integratedsecurity = true");
          String  sql = "Select * from products";
          stmt = conn.prepareStatement(sql);
          // Extract all the records to see the updated records
           rs  = stmt.executeQuery();
          while (rs.next()) {
              // Retrieve data by column name in your database
            a.add(rs.getString("ProductName"));
  
          }
          rs.close();
          } catch (SQLException se) {
              se.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      return a;
  }
  </code></pre>
  
  <p>Will this one is good or not? Then in displaying it..</p>
  
  <pre><code>ArrayList&lt;String&gt; a = new ArrayList&lt;String&gt;();
          String productName = !request.getParameter("productName").isEmpty() ? request.getParameter("productName") : "Empty";
          d.addProduct(productName);
          a = d.get();
  
          Iterator&lt;String&gt; it = a.iterator();
  
          while(it.hasNext()){%&gt;
              &lt;% String baba = (String)it.next(); %&gt;
              &lt;p&gt;Product:  &lt;%= baba %&gt;&lt;/p&gt;
          &lt;%} %&gt;
  </code></pre>
  
  <p>I am wondering if there's any good set-up in doing this and much more easy to understand? Any help/suggestions would be appreciate! Thank you.</p>