私は自分の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();
%>
私はいくつかの方法を試してみました。まず、この方法のために、私は使用がどの行、それは常にドロップダウンリストで毎年取得:第二に
<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>
を第三に、私は入力ジャーとの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>
申し訳ありません私はフォームのドロップダウンリストをいくつか持っているため、ドロップダウンリストがいくつか異なっています。