stmt5 = conn.prepareStatement("SELECT * FROM " + comment_table
stmt = conn.prepareStatement("SELECT * FROM " + comment_table
  public void doGet(HttpServletRequest request, HttpServletResponse response)      throws IOException, ServletException  {    ServletPrinter    sp   = null;    PreparedStatement stmt = null, stmt3 = null, stmt4 = null, stmt5 = null;    Connection        conn = null;    String            categoryName, nickname, title = null, body = null,         category, table;    String            password = null, date = null, username = null;    int               userId, access, storyId = 0;    ResultSet         rs = null, rs3 = null, rs5, count_result;    String            comment_table = null;    String            storyIdtest = request.getParameter("storyId");    sp = new ServletPrinter(response, "ViewStory");    if (storyIdtest == null)    {      sp.printHTML("You must provide a story identifier!<br>");      return;    }    if (storyIdtest != null)    {      storyId = (Integer.valueOf(request.getParameter("storyId"))).intValue();    }    conn = getConnection();    try    {      stmt = conn.prepareStatement("SELECT * FROM stories WHERE id=" + storyId);      rs = stmt.executeQuery();    }    catch (Exception e)    {      sp.printHTML("ERROR: ViewStory query failed" + e);      closeConnection(stmt, conn);      return;    }    try    {      if (!rs.first())      {        stmt = conn.prepareStatement("SELECT * FROM old_stories WHERE id="            + storyId);        rs = stmt.executeQuery();        comment_table = "old_comments";      }      else      {        comment_table = "comments";      }      if (!rs.first())      {        sp            .printHTML("<h3>ERROR: Sorry, but this story does not exist.</h3><br>\n");      }      username = sp.getUserName(rs.getInt("writer"), conn);      date = rs.getString("date");      title = rs.getString("title");      body = rs.getString("body");    }    catch (Exception e)    {      sp.printHTML("Exception viewing story " + e + "<br>");      closeConnection(stmt, conn);      return;    }    sp.printHTMLheader("RUBBoS: Viewing story " + title);    sp.printHTMLHighlighted(title);    sp.printHTML("Posted by " + username + " on " + date + "<br>\n");    sp.printHTML(body + "<br>\n");    sp        .printHTML("<p><center><a href=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="            + comment_table            + "&storyId="            + storyId            + "&parent=0\">Post a comment on this story</a></center><p>");    // Display filter chooser header    sp.printHTML("<br><hr><br>");    sp        .printHTML("<center><form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment\" method=POST>\n"            + "<input type=hidden name=commentId value=0>\n"            + "<input type=hidden name=storyId value="            + storyId            + ">\n"            + "<input type=hidden name=comment_table value="            + comment_table            + ">\n" + "<B>Filter :</B>&nbsp&nbsp<SELECT name=filter>\n");    int i = -1, rating;    String count;    int filter, display;    try    {      stmt4 = conn          .prepareStatement("SELECT rating, COUNT(rating) AS count FROM "              + comment_table + " WHERE story_id=" + storyId              + " GROUP BY rating ORDER BY rating");      count_result = stmt4.executeQuery();      while (count_result.next())      {        rating = count_result.getInt("rating");        count = count_result.getString("count");        filter = 0;        while ((i < 6) && (rating != i))        {          if (i == filter)            sp.printHTML("<OPTION selected value=\"" + i + "\">" + i                + ": 0 comment</OPTION>\n");          else            sp.printHTML("<OPTION value=\"" + i + "\">" + i                + ": 0 comment</OPTION>\n");          i++;        }        if (rating == i)        {          if (i == filter)            sp.printHTML("<OPTION selected value=\"" + i + "\">" + i + ": "                + count + " comments</OPTION>\n");          else            sp.printHTML("<OPTION value=\"" + i + "\">" + i + ": " + count                + " comments</OPTION>\n");          i++;        }      }    }    catch (Exception e2)    {      sp.printHTML("count_result failed " + e2 + "<br>");    }    while (i < 6)    {      sp.printHTML("<OPTION value=\"" + i + "\">" + i          + ": 0 comment</OPTION>\n");      i++;    }    sp        .printHTML("</SELECT>&nbsp&nbsp&nbsp&nbsp<SELECT name=display>\n"            + "<OPTION value=\"0\">Main threads</OPTION>\n"            + "<OPTION selected value=\"1\">Nested</OPTION>\n"            + "<OPTION value=\"2\">All comments</OPTION>\n"            + "</SELECT>&nbsp&nbsp&nbsp&nbsp<input type=submit value=\"Refresh display\"></center><p>\n");    display = 1;    filter = 0;    try    {      stmt5 = conn.prepareStatement("SELECT * FROM " + comment_table          + " WHERE story_id=" + storyId + " AND parent=0 AND rating>="          + filter);      rs5 = stmt5.executeQuery();      String subject, comment, writer, link;      int childs, parent, id;      if (rs5.first())      {        do        {          username = sp.getUserName(rs5.getInt("writer"), conn);          subject = rs5.getString("subject");          rating = rs5.getInt("rating");          date = rs5.getString("subject");          comment = rs5.getString("comment");          id = rs5.getInt("id");          parent = rs5.getInt("parent");          childs = rs5.getInt("childs");          sp.printHTML("<br><hr><br>");          sp              .printHTML("<TABLE width=\"100%\" bgcolor=\"#CCCCFF\"><TR><TD><FONT size=\"4\" color=\"#000000\"><B><a href=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="                  + comment_table                  + "&storyId="                  + storyId                  + "&commentId="                  + id                  + "&filter="                  + filter                  + "&display="                  + display                  + "\">"                  + subject                  + "</a></B>&nbsp</FONT> (Score:"                  + rating                  + ")</TABLE>\n");          sp.printHTML("<TABLE><TR><TD><B>Posted by " + username + " on "              + date + "</B><p>\n");          sp.printHTML("<TR><TD>" + comment);          sp              .printHTML("<TR><TD><p>[ <a href=\"/rubbos/servlet/edu.rice.rubbos.servlets.PostComment?comment_table="                  + comment_table                  + "&storyId="                  + storyId                  + "&parent="                  + id                  + "\">Reply to this</a>&nbsp|&nbsp"                  + "<a href=\"  /rubbos/servlet/edu.rice.rubbos.servlets.ViewComment?comment_table="                  + comment_table                  + "&storyId="                  + storyId                  + "&commentId="                  + parent                  + "&filter="                  + filter                  + "&display="                  + display                  + "\">Parent</a>"                  + "&nbsp|&nbsp<a href=\"/rubbos/servlet/edu.rice.rubbos.servlets.ModerateComment?comment_table="                  + comment_table                  + "&commentId="                  + id                  + "\">Moderate</a> ]</TABLE>\n");          if (childs > 0)              display_follow_up(id, 1, display, filter, conn, comment_table,                                sp, conn);        }        while (rs5.next());      }    }    catch (Exception e)    {      sp.printHTML("Failed to execute Query for ViewStory: " + e);      closeConnection(stmt, conn);      return;    }    closeConnection(stmt, conn);    sp.printHTMLfooter();  }