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 StringBuffer
and print that all out in one fell swoop. You'll save on objects, too. Your code becomes:
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();
}
Try putting an out.flush()
at the end of your code. It's likely that the data is just sitting in a buffer waiting to be sent when you run the code normally.
精彩评论