Java 在下拉列表中显示从数据库中选择的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21523751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:09:52  来源:igfitidea点击:

Display Selected Data from database in drop down list

javadatabaseservletsdrop-down-menuforeach

提问by newbieinjavaversion2

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).

例如,下图显示 fbMenuId = M001(千层面)。

database1

数据库1

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

所以在下拉列表中 M001 选项将被选中。我还需要显示其他菜单,如 M002、M003、M004、M005、M006 和 M007。例如,请参见下图

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!

我可以展示 M001 千层面。但是,我不想要 2 个千层面。任何人请帮助我。帮助将不胜感激。提前致谢!

Below are codes for servlet and data access object.

下面是 servlet 和数据访问对象的代码。

Servlet

小服务程序

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

OrderDAO

订单DAO

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;
    }

采纳答案by newbieinjavaversion2

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

回答by Mahesh Modukuru

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>