我尝试过'while循环'。但它只显示一行。 谁能告诉我如何解决这个问题?
以下是我使用的代码:
<%
//oracle Connection.....
String a=select * from tblname where userid='"+userid+"' //with same userid for multiple records saved in db
Resultset rs=st.executeQuery(a);
while(rs.next)
{
%>
<li><option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> //retrived only for single row
<%
}
%>
答案 0 :(得分:0)
我可以举个简单的例子。在此示例中,我使用JSTL。
<强>的Servlet 强>
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
CustomerDao customerDao = new CustomerDao();
List<Customer> customers = customerDao.getCustomers();
request.setAttribute("customers", customers);
request.getRequestDispatcher("jsp/customers.jsp").forward(request,
response);
}
<强> JSP:强>
<select>
<c:forEach var="customer" items="${customers}">
<option value="${customer.id}">${customer.name}</option>
</c:forEach>
</select>
答案 1 :(得分:0)
对于下拉菜单,您需要使用<select>
标记。请尝试这样:
<%
//oracle Connection.....
String a="select * from tblname where userid='"+userid+"'";
Resultset rs=st.executeQuery(a);
%><select><%
while(rs.next())
{
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
还要确保查询会产生多行,否则您将获得一行或者可能没有行。