【问题标题】:Set drop down list's selected value according to the database retrieved根据检索到的数据库设置下拉列表的选择值
【发布时间】:2017-05-26 14:19:44
【问题描述】:

我正在尝试在我的 jsp 中做一个更新表单。我需要帮助将下拉列表显示设置为从数据库中检索到的值。所以当用户想要编辑某一行数据时,只需点击对应的更新按钮,数据就会显示在表单中。

我可以使用input type="text" name="expenseTitle" style="margin-left:12px" value="<%=rec.getString("expense_title")%>">将数据检索到我的文本框中。

我正在使用此连接来连接和检索我的数据库:

<%
    Connection connect = null;
    Statement s = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");

        connect =  DriverManager.getConnection("jdbc:mysql://localhost:3306/asldb" + "?user=root&password=mysql");

        s = connect.createStatement();
         String id = request.getParameter("id");  

        String sql = "SELECT * FROM input_expense WHERE id = '" + id +"'"; 

        ResultSet rec = s.executeQuery(sql);
        if(rec != null) {
            rec.next();

%>

我尝试了几种方法。首先,对于这种方法,无论我使用哪一行,它总是在下拉列表中检索 Yearly:

<select id="LT_occurrenceDDL" class="LT_formDDL" name="expenseOccurrence">
                    <option value="-1">Select an option</option>
                    <option value="One-Time" selected="<%=rec.getString("payment_occurrence").equals("One-Time")%>">One-Time</option>
                    <option value="Daily" selected="<%=rec.getString("payment_occurrence").equals("Daily")%>">Daily</option>
                    <option value="Weekly" selected="<%=rec.getString("payment_occurrence").equals("Weekly")%>">Weekly</option>
                    <option value="Monthly" selected="<%=rec.getString("payment_occurrence").equals("Monthly")%>">Monthly</option>
                    <option value="Quarterly" selected="<%=rec.getString("payment_occurrence").equals("Quarterly")%>">Quarterly</option>
                    <option value="Yearly" selected="<%=rec.getString("payment_occurrence").equals("Yearly")%>">Yearly</option>
                </select>

其次:

<select class="LT_formDDL" name="expenseCategory"">
                    <option value="-1">Select a category</option>
                    <option value="mortgage/rent" <%= (rec.getString("expense_category")=="Mortgage/Rent Payment"?"selected='selected'":"")%>>Mortgage/Rent Payment</option>
                    <option value="loan" <%= (rec.getString("expense_category")=="Loans"?"selected='selected'":"")%>>Loans</option>
                    <option value="insurance" <%= (rec.getString("expense_category")=="Insurance"?"selected='selected'":"")%>>Insurance</option>
                    <option value="utilities" <%= (rec.getString("expense_category")=="Utilities"?"selected='selected'":"")%>>Utilities</option>
                    <option value="groceries" <%= (rec.getString("expense_category")=="Groceries"?"selected='selected'":"")%>>Groceries</option>
                    <option value="food" <%= (rec.getString("expense_category")=="Food"?"selected='selected'":"")%>>Food</option>
                    <option value="clothing" <%= (rec.getString("expense_category")=="Clothing"?"selected='selected'":"")%>>Clothing</option>
                    <option value="entertainment" <%= (rec.getString("expense_category")=="Entertainment"?"selected='selected'":"")%>>Entertainment</option>
                    <option value="others" <%= (rec.getString("expense_category")=="Others"?"selected='selected'":"")%>>Others</option>
                </select>

第三,我有输入jar和taglib:

<select id="LT_occurrenceDDL"class="LT_formDDL" name="expenseOccurrence">
                    <option value="-1">Select an option</option>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "One-Time"}'>
                    <option value="One-Time" selected>One-Time</option>
                    </c:when>
                    <c:otherwise>
                    <option value="One-Time">One-Time</option>
                    </c:otherwise>
                    </c:choose>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "Daily"}'>
                    <option value="Daily" selected>Daily</option>
                    </c:when>
                    <c:otherwise>
                    <option value="Daily">Daily</option>
                    </c:otherwise>
                    </c:choose>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "Weekly"}'>
                    <option value="Weekly" selected>Weekly</option>
                    </c:when>
                    <c:otherwise>
                    <option value="Weekly">Weekly</option>
                    </c:otherwise>
                    </c:choose>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "Monthly"}'>
                    <option value="Monthly" selected>Monthly</option>
                    </c:when>
                    <c:otherwise>
                    <option value="Monthly">Monthly</option>
                    </c:otherwise>
                    </c:choose>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "Quarterly"}'>
                    <option value="Quarterly" selected>Quarterly</option>
                    </c:when>
                    <c:otherwise>
                    <option value="Quarterly">Quarterly</option>
                    </c:otherwise>
                    </c:choose>
                    <c:choose>
                    <c:when test='${rec.getString("payment_occurrence") == "Yearly"}'>
                    <option value="Yearly" selected>Yearly</option>
                    </c:when>
                    <c:otherwise>
                    <option value="Yearly">Yearly</option>
                    </c:otherwise>
                    </c:choose>
                </select>

抱歉,有些人的下拉列表不一样,因为我在表单中有几个下拉列表,因此我尝试了不同的一次使用不同的方法。

【问题讨论】:

    标签: java html mysql jsp servlets


    【解决方案1】:

    对我来说,你应该创建两个 .jsp 页面来做你想做的事。第一个将显示您使用updatecreate 按钮拥有的所有用户,第二个将显示您要更新的用户(使用当前id)。您可以仅使用 Ajax 转发您的请求而无需更新主页。更多信息请看这个测试项目test

    【讨论】:

      【解决方案2】:

      看来您只使用第一行结果集而没有迭代。你应该像这样迭代结果集 -

      if(rec!=null){
        while(rec.next()){
           // your retrieval code 
        }
      }
      

      我认为这可能会解决问题

      【讨论】:

        猜你喜欢
        • 2016-07-03
        • 2021-04-14
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多