This is strange… I’m displaying results in an HTML table but I’m getting a different number of results being displayed depending on if/how I am debugging. If I debug slowly and hit each line then all of the results show. If I just run it or don’t step through each line then I only get one result in the table. Either way the result set does have the correct number of rows, they just aren’t being displayed in the table correctly.
Does anyone have any ideas why this strange behavior is happening? I’m using Eclipse Indigo. Below is the block of code that I’m using to select the records and display them.
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();
}
Try appending all the output to a
StringBufferand print that all out in one fell swoop. You’ll save on objects, too. Your code becomes: