使用数据库中存在的值填充jsp下拉菜单

时间:2015-05-22 06:46:07

标签: database jsp servlets

如何使用数据库中存在的值(在MVC架构之后)填充JSP中的下拉菜单? 试过这样做 内 EmployeeDAO.java

private List<EmployeeVO> empIDList;

    public List<EmployeeVO> getStateList(){
        empIDList=new ArrayList<EmployeeVO>();
        Connection con=DBUtil.getConnection();



        try {
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select Emp_id from Employee order by Emp_id ASC");
            while(rs.next()){
                String emID=rs.getString("Emp_id");
                int empID=Integer.parseInt(emID);
                EmployeeVO s=new EmployeeVO();
                s.setEmp_id(empID); 
                empIDList.add(s);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return empIDList;
    }

内部 EmployeeVO.java

private int Emp_id;
    private String Emp_pwd;
    private String Emp_fname;
    private String Emp_mname;
    private String Emp_lname;
    private String Emp_dob;
    private String Emp_doj;
    private String Emp_status;
    public int getEmp_id() {
        return Emp_id;
    }
    public void setEmp_id(int emp_id) {
        Emp_id = emp_id;
    }

内部 Employee.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="emplID" scope="session" class="com.model.EmployeeVO" />

<th>Employee ID</th>
                    <td><input type="text" name="EID" id="eid" /> 
                    <select
                        id="testlist">
                            <c:forEach var="item" items="${emplID.items}">
                                <option>${item}</option>
                            </c:forEach>
                    </select> 
                    </td>

jsp的源代码来自this页面

1 个答案:

答案 0 :(得分:2)

也许您可以尝试这种Vectorarraylist方法..

jsp part(vector)

<%
  Vector vFRUIT_CODE    = new Vector();
  Vector vFRUIT_DESCP   = new Vector();

  String SQL = "SELECT * FROM TB_TEST";
  DB_TEST.makeConnection();
  DB_TEST.executeQuery(SQL);
  while(DB_TEST.getNextQuery())
  {
           String sCODE    = DB_TEST.getColumnString("FRUIT_CODE");
           String sDESCP   = DB_TEST.getColumnString("FRUIT_DESCP");

          vFRUIT_CODE.addElement(sCODE);
          vFRUIT_DESCP.addElement(sDESCP);
  }
  DB_TEST.takeDown();
%>

html部分

<select name="FRUIT">
<option value="">--- Please Select ---</option>
<%
    for (int i=0;i<vFRUIT_CODE.size();i++) {
          String sCODE = (String) vFRUIT_CODE.elementAt(i);
          String sDESCP = (String) vFRUIT_DESCP.elementAt(i);
 %>      
<option value="<%=sCODE %>" ><%= sDESCP %></option>
<%}%>  
</select>