简体   繁体   中英

How to retrieve multiple set of values from db to a drop down in jsp page

I've tried with 'while loop'. but it displays only one row. can someone tell me how to resolve the issue?

following is the code i've used :

<%    
 //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
<%
}
%>

I can give you a simple example. In this example I use 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>

For drop down you need to use <select> tag.Try like this:

 <%    
  //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>

Also make sure that the query results in more than one row or else you will be getting one or may be no row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM