簡體   English   中英

根據檢索到的數據庫設置下拉列表的選定值

[英]Set drop down list's selected value according to the database retrieved

我正在嘗試在我的 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>

抱歉,有些人的下拉列表不同,因為我在表單中有幾個下拉列表,因此我嘗試了不同的方法,使用了不同的方法。

至於我,您應該創建兩個 .jsp 頁面來執行您想要的操作。 第一個將顯示您擁有update所有用戶並create按鈕,第二個將顯示您要更新的用戶(使用當前id )。 您可以僅使用 Ajax 轉發您的請求,而無需更新主頁。 更多信息請看這個測試項目測試

看來您只使用結果集的第一行而沒有迭代。 你應該像這樣迭代結果集 -

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

我認為這可能會解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM