我有一个名为" payroll_items"的sql表。如您所见,有三行数据。您还可以在下面看到我的代码,这是一个java servlet,它从数据库中检索数据并以表格格式显示它。但是,正如您在表格格式中看到的那样,它只显示sql表的第一行(SA,Superannuation等...),而不显示其他两行(ST和WA)。如何以表格格式显示所有值?它只显示第一行。
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
PreparedStatement ps = con.prepareStatement("select ItemCode, ItemName, ItemDescription, Rate from payroll_system.payroll_items");
ResultSet rs = ps.executeQuery();
st = rs.next();
if(st) {
out.println("<tr>");
out.println("<td><h3>Item Code</h3></td>");
out.println("<td><h3>Item Name</td></h3>");
out.println("<td><h3>Item Description</h3></td>");
out.println("<td><h3>Rate</h3></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>"+ rs.getString("ItemCode")+"</td>");
out.println("<td>"+ rs.getString("ItemName")+"</td>");
out.println("<td>"+ rs.getString("ItemDescription")+"</td>");
out.println("<td>"+ rs.getString("Rate")+"</td>");
out.println("</tr>");
答案 0 :(得分:1)
迭代ResultSet
。您忘记在ResultSet
ResultSet rs = ps.executeQuery("select ItemCode, ItemName, ItemDescription, Rate from payroll_system.payroll_items");
while (rs.next()) {
out.println("<tr>");
out.println("<td>"+ rs.getString("ItemCode")+"</td>");
out.println("<td>"+ rs.getString("ItemName")+"</td>");
out.println("<td>"+ rs.getString("ItemDescription")+"</td>");
out.println("<td>"+ rs.getString("Rate")+"</td>");
out.println("</tr>");
}
答案 1 :(得分:0)
在您的情况下,Resultset有3个索引,您应该使用
打印所有索引while (rs.next()) {
out.println("<td>"+ rs.getString("ItemCode")+"</td>");
out.println("<td>"+ rs.getString("ItemName")+"</td>");
out.println("<td>"+ rs.getString("ItemDescription")+"</td>");
out.println("<td>"+ rs.getString("Rate")+"</td>");
}