Question: I have a simple statement in a servlet file that will go through the database and get the result set

```
ResultSet AssignmentList = stmt.executeQuery("SELECT * FROM Assignments WHERE " + projectID + "= Pid;");
request.setAttribute("assignmentList", AssignmentList);

```

As you can see I'm sending the `ResultSet` to a JSP file. And in that JSP file I tried to iterate through it like this.

```
<table class="CSSTableGenerator">
    <thead>
    <tr>
        <th>Aid</th>
        <th>Title</th>
        <th>Task</th>
        <th>ID of User Responsible</th>
    </tr>
    </thead>
    <tbody>

    <c:forEach items="${assignmentList}" var="item">
        <tr>
            <td>${item.Aid}</td>
            <td>${item.Title}</td>
            <td>${item.Task}</td>
            <td> ${item.UserID}</td>            
        </tr>
    </c:forEach>

    </tbody>
</table>

```

but when I do that I get an error. 
Am I suppose to iterating in wrong way, or is it not possible to iterate through a `ResultSet`.

Answer: 
After we read the ResultSet, we need to close the database connection. Because you read the ResultSet in JSP, you can only close the connection in JSP. This, in my opinion, is not a good practice.

After you query the database in the servlet, read the ResultSet, store the result in a List, and then close the database connection. Then, pass this List to JSP in a request attribute. Your JSP retrieve the List from request attribute using EL. You can use JSTL to iterate over this List.
Assuming you have a fairly standard `ItemBean` (I made everything a String, except projectId). Then, I would probably start with a [`PreparedStatement`](http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html) in a DAO helper method like so

```
private static List<ItemBean> queryForItems(
    Connection conn, int projectId) {
  PreparedStatement ps = null;
  ResultSet rs = null;
  // The Query is using a bind parameter
  final String ITEM_SQL = "SELECT * FROM Assignments WHERE Pid = ?";

  // This is the Diamond Operator... if pre Java 7, use new ArrayList<ItemBean>();
  List<ItemBean> al = new ArrayList<>();
  try {
    // Prepare the statement
    ps = conn.prepareStatement(ITEM_SQL);
    ps.setInt(1, projectId); // <-- bind the parameter
    rs = ps.executeQuery();
    while (rs.next()) { // <-- "Iterate" the ResultSet
      ItemBean ib = new ItemBean();
      ib.setPid(projectId);
      ib.setAid(rs.getString("Aid"));
      ib.setTitle(rs.getString("Title"));
      ib.setTask(rs.getString("Task"));
      ib.setUserId(rs.getString("UserID"));
    }
  } catch (SQLException se) {
    System.err.println("Error with " + ITEM_SQL + " and projectId = " + projectId);
    se.printStackTrace(System.err);
  } finally {
    // Clean up!
    try {
      rs.close();
    } catch (SQLException ignored) {
    }
    try {
      ps.close();
    } catch (SQLException ignored) {
    }
  }
  return al;
}

```