newbieinjavaversion2
newbieinjavaversion2

Reputation: 499

Display Selected Data from database in drop down list

My goal is to selected data in database and display in drop down list.

For example, see the image below show that fbMenuId = M001 (Lasagne).

database1

So at dropdownlist M001 option will be selected. I also need display OTHER MENU like M002,M003,M004,M005,M006 and M007. For example, see the image below

database

However, my outcome is

ddl

Below are my codes

<select class="form-control" name="menu" id="menu">
                                            <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option>
                                            <c:forEach var="menu" items="${menu}">
                                                <option value="${menu.fbMenuId}">${menu.fbMenuName}</option>
                                            </c:forEach>

</select>

I am able to display M001 Lasagne. However, there are 2 Lasagne which I do not want. Anyone please help me. Help will be appreciate. Thanks in advance!

Below are codes for servlet and data access object.

Servlet

OrderDAO dao = new OrderDAO();
request.setAttribute("order", dao.getOrder(fbOrderId));
request.setAttribute("menu", dao.getMenu(restaurant));

OrderDAO

public OrderBean getOrder(Integer fbOrderId) {

            OrderBean ob = new OrderBean();
            try {
                currentCon = ConnectionManager.getConnection();
                Statement statement = currentCon.createStatement();
                ResultSet rs = statement.executeQuery("SELECT fborders.fbMenuId, fbMenuName FROM fborders INNER JOIN fbmenu ON fborders.fbMenuId = fbmenu.fbMenuId WHERE fbOrderId='"+ fbOrderId + "'");

                while (rs.next()) {
                    ob.setFbMenuId(rs.getString("fbMenuId"));
                    ob.setFbMenuName(rs.getString("fbMenuName"));
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

            return ob;
        }



public ArrayList getMenu(String restaurant) {

        ArrayList<OrderBean> am = new ArrayList<OrderBean>();
        try {
            currentCon = ConnectionManager.getConnection();
            Statement statement = currentCon.createStatement();
            ResultSet rs = statement
                    .executeQuery("SELECT fbMenuId, fbMenuName FROM fbmenu WHERE fbRestaurantId='"
                            + restaurant + "'");

            while (rs.next()) {
                OrderBean ob = new OrderBean();
                ob.setFbMenuId(rs.getString("fbMenuId"));
                ob.setFbMenuName(rs.getString("fbMenuName"));
                am.add(ob);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return am;
    }

Upvotes: 1

Views: 21105

Answers (2)

newbieinjavaversion2
newbieinjavaversion2

Reputation: 499

<select class="form-control" name="menu" id="menu">
    <c:forEach var="menu" items="${menu}">
        <c:choose>
            <c:when test="${menu.fbMenuId == order.fbMenuId}">
                <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option> 
            </c:when>
            <c:otherwise>
                <option value="${menu.fbMenuId}">${menu.fbMenuName} </option>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</select>

Upvotes: 0

Mahesh Modukuru
Mahesh Modukuru

Reputation: 51

As per my understanding, you are showing the selected value twice. one time by appending

  <option value="${order.fbMenuId}" selected>${order.fbMenuName}</option>

and another time by iterating the list. Instead of this populate all the values in the dropdown and set the required value as selected. Just write simple condition like the following.

   <select class="form-control" name="menu" id="menu">         
      <c:forEach var="menu" items="${menu}">
          <option value="${menu.fbMenuId}">${menu.fbMenuName} 
          <c:if test="${menu.fbMenuId == order.fbMenuId}">
           selected
           </c:if>
          </option>
      </c:forEach>
   </select>

Upvotes: 2

Related Questions