我正在尝试在我的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>
很抱歉,下拉列表对于某些人来说是不同的,因为我在表单中有一些下拉列表,因此我尝试使用不同的方法进行了一次不同。
答案 0 :(得分:0)
至于我,你应该创建两个.jsp页面来做你想要的。第一个将显示您拥有update
和create
按钮的所有用户,第二个将显示您要更新的用户(使用当前id
)。您只需使用Ajax即可转发请求而无需更新主页。有关详细信息,请查看此测试项目test
答案 1 :(得分:0)
看来你只是在没有迭代的情况下使用结果集的第一行。你应该像这样迭代结果集 -
if(rec!=null){
while(rec.next()){
// your retrieval code
}
}
我认为这可能会解决问题