多条件检索功能的实现

package cn.com.servlet;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.gj.Person_info;
import cn.com.lf.JdbcUtils;
public class SearchCondition extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
    //这个servlet的主要作用是根据用户的选择才查询出想要的结果
	String name=request.getParameter("name");
	name = URLEncoder.encode(name, "ISO-8859-1");
	name = URLDecoder.decode(name, "UTF-8");
	String num=request.getParameter("num");	
	/*String duty=request.getParameter("duty");
	duty = URLEncoder.encode(duty, "ISO-8859-1");
	duty = URLDecoder.decode(duty, "UTF-8");
	String kinds=request.getParameter("kinds");
	kinds = URLEncoder.encode(kinds, "ISO-8859-1");
	kinds = URLDecoder.decode(kinds, "UTF-8");
	String part=request.getParameter("part");
	part = URLEncoder.encode(part, "ISO-8859-1");
	part = URLDecoder.decode(part, "UTF-8");
	String state=request.getParameter("state");
	state = URLEncoder.encode(state, "ISO-8859-1");
	state = URLDecoder.decode(state, "UTF-8");*/
	//1.1把收到的信息都存储到一个集合里面,如果是空的就筛选掉,放进一个新的集合
	//penson(假如你的数据放在对象中)
	Map map =new HashMap();
	String sql =" select * from person_info where 1=1 ";
	Person_info person=new Person_info();
	if(name!= null){
	map.put("name",name);
	}else if(num!=null){
		map.put("num",num);
	}
	else if(duty!=null){
		map.put("duty",duty);
	}
	else if(kinds!=null){
		map.put("kinds",kinds);
	}
	
	else if(part!=null){
		map.put("part",part);	
	}
	else if(state!=null){
		map.put("state",state);	
	}
	Set key = map.keySet(); 
	for (String i : key) {
	sql=sql+"and "+i+"= '"+map.get(i)+"'";
	System.out.println("key:"+key);
	}
	System.out.println("sql:"+sql);
   ResultSet rs=JdbcUtils.select(sql, null);
   List list=new ArrayList();
  try {
	while(rs.next()){ 
		 Person_info pin=new Person_info(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
		 list.add(pin);		 
	  }
	request.setAttribute("list", list);
	request.getRequestDispatcher("/Personnel_definition.jsp").forward(request, response);
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	//1.2根据新的集合的来进行查询
	
	}

	

}

你可能感兴趣的:(socket通讯专栏)