How to populate the dropdown list from sql server database using LinkedHashMap in java

北战南征 提交于 2019-12-02 03:32:00

There are two problems with your code. First, every reference mapping needs to have its own List. Currently, the list is being shared and hence both the reference entries would show all the dates. Change the code as

rs = stmt.executeQuery(sql);
while (rs.next()) 
{
    List<String> list = new ArrayList<String>();
    list.add(rs.getString(2));
    list.add(rs.getString(3));
    ref.put(rs.getString(1), list);
}

The second problem is with the JSTL tag. You need only one <option> tag within the loop which would run twice for you (because you have two reference entries coming from the database) to generate the two <option> tag entries.

<select name="ref_logtime">
  <c:forEach var="aff" items="${obj.connect()}">
    <option value="${aff.key}">${aff.key} ${aff.value}</option>
  </c:forEach>
</select>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!