为什么我的查询结果在调试时显示不同?

时间:2011-07-24 00:04:18

标签: java eclipse debugging

这很奇怪......我在HTML表格中显示结果但是我根据是否/如何调试显示不同数量的结果。如果我慢慢调试并点击每一行,那么所有结果都显示出来。如果我只是运行它或不遍历每一行,那么我只在表中得到一个结果。无论哪种方式,结果集都有正确的行数,它们只是没有正确显示在表中。

有没有人有任何想法为什么会发生这种奇怪的行为?我正在使用Eclipse Indigo。下面是我用来选择记录并显示它们的代码块。

      try {
     String query =
     "Select * from plants where name = '"
     + name + "'";
     String plantName = "";
     ResultSet rs = sttmnt.executeQuery(query);

 while (rs.next()) { // display information for each plant.
        plantName = rs.getString(2);  // display fields in cells
        out.println("<tr><td>");
        out.println(plantName + "</td><td>");
        out.println(rs.getString(3) + "</td><td>");
        out.println("$" + rs.getString(5) + "</td><td>");
        out.println(rs.getString(4) + "</td>");
        out.println("<input type=\"hidden\" name=\"plantName" +
         plantNo + "\" value=\"" + plantName + "\">");
        out.println("<input type=\"hidden\" name=\"plantID" +
         plantNo + "\" value=\"" + rs.getString(1) + "\">");
        out.println("</tr>");
        plantNo++;
     }
     if (plantNo == 0) out.println("<tr><td align=\"center\" " +
          " colspan=\"4\">Sorry, there are currently no " + name
          + " plants for sale.</td></tr>");
     else
         out.println("<tr><td align=\"center\" " +
                 " colspan=\"4\">Showing " + plantNo
                 + " results. </td></tr>");

     out.println("</table>");
     rs.close();
  }

2 个答案:

答案 0 :(得分:0)

尝试在代码末尾添加out.flush()。当您正常运行代码时,数据可能只是等待发送的缓冲区。

答案 1 :(得分:0)

尝试将所有输出附加到StringBuffer并一次性打印出所有输出。你也可以节省对象。您的代码变为:

try {
 String query = "Select * from plants where name = ?";
 sttmnt.setString(1, name);
 String plantName = "";
 ResultSet rs = sttmnt.executeQuery(query);

 StringBuffer output = new StringBuffer(); 
 while (rs.next()) { // display information for each plant.
    plantName = rs.getString(2);  // display fields in cells
    output.append("<tr><td>");
    output.append(plantName + "</td><td>");
    output.append(rs.getString(3) + "</td><td>");
    output.append("$" + rs.getString(5) + "</td><td>");
    output.append(rs.getString(4) + "</td>");
    output.append("<input type=\"hidden\" name=\"plantName" +
     plantNo + "\" value=\"" + plantName + "\">");
    output.append("<input type=\"hidden\" name=\"plantID" +
     plantNo + "\" value=\"" + rs.getString(1) + "\">");
    output.append("</tr>");
    plantNo++;
 }
 if (plantNo == 0) output.append("<tr><td align=\"center\" " +
      " colspan=\"4\">Sorry, there are currently no " + name
      + " plants for sale.</td></tr>");
 else
     output.append("<tr><td align=\"center\" " +
             " colspan=\"4\">Showing " + plantNo
             + " results. </td></tr>");

 output.append("</table>");
 out.println(output.toString());
 rs.close();
}