一,
根据 传入的cellphone值在数据库中的进行查找:
public List<Customer> selectBycell(String cellphone) {
if(cellphone==null)
throw new IllegalArgumentException();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Customer> cs= new ArrayList<Customer>();
try{
conn=JdbcUtil.getConnection();
stmt = conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from customer where cellphone=?");
stmt.setString(1,cellphone);
rs = stmt.executeQuery();
while(rs.next()){
Customer c = new Customer();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setBirthday(rs.getDate("birthday"));
c.setCellphone(rs.getString("cellphone"));
c.setEmail(rs.getString("email"));
c.setHobby(rs.getString("hobby"));
c.setType(rs.getString("type"));
c.setDescription(rs.getString("description"));
cs.add(c);
}
return cs;
}catch(Exception e){
throw new DaoException(e);
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
}
二、servlet中的的方法
private void selectcell(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String selecbyell =request.getParameter("cellphone");
cs.selectByCell(selecbyell);
request.setAttribute("selecbyell", selecbyell);
request.getRequestDispatcher("/selectcustomer.jsp").forward(request, response);
}
三、在foreach中动态获取的下拉列表中,选中一个字段进行查找,这个字段可以对应多个值
<form method="post" action="${pageContext.request.contextPath}/servlet/SelectBycell?operation=selectcell">
<select name="cellphone" id="cell" align="center">
<option value="">==按电话查询==</option>
<c:forEach items="${cellphone}" var="cell" varStatus="vs">
<option value="${cell}"><span align="center">${cell}</span></option>
</c:forEach>
</select>
<input type="image" id="submit" src="<c:url value='/images/select1.png'/>" class="loginBtn"/>
</form>
select的onchange事件。选中时js调用查询方法、查询后list返回页面。遍历循环即可
多谢了
你把多个值在查询时变成集合,就可以foreach啦。
多谢了 ,已经弄出来了