如何通过搜索语句JSP使用Dropdown列表从mysql中检索值?

时间:2014-07-31 07:42:22

标签: mysql jsp

jsp page

这是jsp页面,其中应用了下拉列表选项以检索和添加值集

   <tr>
    <td>&nbsp;</td>
    <td width="25%">Vehicle Type :-</td>
    <td width="25%">
      <select value="${veh_type}" style="width:115px;" name="slct_V_type" id="slct_V_type">         
        <option>Select a Type</option>
        <option value="Wagon">Wagon</option>
        <option value="Truck">Truck</option>
        <option value="Bus">Bus</option>
        <option value="SUV">SUV</option>
        <option value="Sedan">Sedan</option>
        <option value="Minivan">Minivan</option>
        <option value="Luxury">Luxury</option>
        <option value="Hybrid">Hybrid</option>
        <option value="Hatchback">Hatchback</option>
        <option value="Coupe">Coupe</option>
      </select></td>
    <td width="25%">&nbsp;</td>

    </tr>

     <td width="25%"><input style="width:100px;" type="submit" name="bttnsrch" id="bttnsrch" value="Search" /></td>

的Servlet

ere是servlet搜索按钮编码,它将数据传递给数据库

 if(request.getParameter("bttnsrch")!=null)
        {
            int Reg=Integer.parseInt(request.getParameter("Reg_number"));
            ResultSet rs=SQL.CaptureVehicleInfo(Reg);
            while(rs.next())
            {

                String veh_type=rs.getString(1);
                request.setAttribute("veh_type", veh_type);

                String reg_num=rs.getString(2);
                request.setAttribute("reg_num", reg_num);

                String veh_brand=rs.getString(3);
                request.setAttribute("veh_brand", veh_brand);

                String veh_model=rs.getString(4);
                request.setAttribute("veh_model", veh_model);

                String veh_man_year=rs.getString(5);
                request.setAttribute("veh_man_year", veh_man_year);

                String no_of_seat=rs.getString(6);
                request.setAttribute("no_of_seat", no_of_seat);

                String trasm=rs.getString(7);
                request.setAttribute("trasm", trasm);

                String air_con=rs.getString(8);
                request.setAttribute("air_con", air_con);
            }
            request.getRequestDispatcher("Vehicle_Information.jsp").forward(request, response);
            response.sendRedirect("Vehicle_Information.jsp");
        }

数据库连接

这是我创建的用于从数据库中搜索车辆信息的SQL语句。

     public ResultSet CaptureVehicleInfo(int Register_number)
   {
       ResultSet rs;
       try
       {
           SQL=createConnection();

           PreparedStatement ps;
           String str="SELECT * FROM vehicle_information WHERE Register_number=?";
           ps=SQL.prepareStatement(str);

           ps.setInt(1, Register_number);


            rs=ps.executeQuery();
            return rs;

       }
       catch(Exception e)
       {
           rs=null;
           return rs;
       }
   }

1 个答案:

答案 0 :(得分:0)

您需要迭代select标记内的值。你可以使用jstl forEach标签轻松完成,如下所示,

 <select style="width:115px;" name="slct_V_type" id="slct_V_type">
       <c:forEach var="temp" items="${veh_type}">
            <option value="${temp.name}">${temp.name}</option>                  
        </c:forEach>
  </select>

不要忘记为jstl支持添加taglib

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

我希望${veh_type}包含对象列表