从Mysql过滤结果集

时间:2014-04-26 15:50:58

标签: java servlets

我有一个名为user_details的表,有4列(User_id,User_Name,Password和Designation)。我在每个列中都有搜索字段,用户可以在其中提供查询,搜索按钮将根据该特定列的查询给出记录。现在我想根据另一列的查询过滤结果集。以下是从每列中获取记录的代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String query;
    String hiddenname = request.getParameter("hiddenname");
    Connection conn;
    Statement stmt;
    ResultSet res;
    DatabaseConnection dbconn;
    List lst=new ArrayList();
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();


    try {

        if(hiddenname.equals("hidden_uid"))
        {

            String id=request.getParameter("uid");
            System.out.println(id);
            dbconn=new DatabaseConnection();
            conn=dbconn.setConnection();

            stmt=conn.createStatement();

            query="select * from user_details where User_id="+id;


            System.out.println(query);
            res=dbconn.getResultSet(query, conn);
            System.out.println(res);

            while(res.next())
            {
                lst.add(res.getString("User_id"));
                lst.add(res.getString("User_Name"));
                lst.add(res.getString("Password"));
                lst.add(res.getString("Designation"));

            }

            res.close();
        }
        else if(hiddenname.equals("hidden_pass"))
        {
            String pass=request.getParameter("pass");
            System.out.println(pass);
            dbconn=new DatabaseConnection();
            conn=dbconn.setConnection();

            stmt=conn.createStatement();

            query="select * from user_details where Password LIKE '%"+pass+"%'";


            System.out.println(query);
            res=dbconn.getResultSet(query, conn);
            System.out.println(res);

            while(res.next())
            {
                lst.add(res.getString("User_id"));
                lst.add(res.getString("User_Name"));
                lst.add(res.getString("Password"));
                lst.add(res.getString("Designation"));

            }

            res.close();
        }

        else if(hiddenname.equals("hidden_uname"))
        {
            String uname=request.getParameter("uname");
            // System.out.println(uname);
            dbconn=new DatabaseConnection();
            conn=dbconn.setConnection();

            stmt=conn.createStatement();

            query="select * from user_details where User_Name LIKE '%"+uname+"%'";


            System.out.println(query);
            res=dbconn.getResultSet(query, conn);
            //System.out.println(res);

            while(res.next())
            {
                lst.add(res.getString("User_id"));
                lst.add(res.getString("User_Name"));
                lst.add(res.getString("Password"));
                lst.add(res.getString("Designation"));

            }

            res.close();
        }
        else if(hiddenname.equals("hidden_desig"))
        {
            String desig=request.getParameter("desig");
            System.out.println(desig);
            dbconn=new DatabaseConnection();
            conn=dbconn.setConnection();

            stmt=conn.createStatement();

            query="select * from user_details where Designation LIKE '%"+desig+"%'";


            System.out.println(query);
            res=dbconn.getResultSet(query, conn);
            System.out.println(res);

            while(res.next())
            {
                lst.add(res.getString("User_id"));
                lst.add(res.getString("User_Name"));
                lst.add(res.getString("Password"));
                lst.add(res.getString("Designation"));

            }

            res.close();
        }

    }
    catch(Exception e) {
        RequestDispatcher rd=request.getRequestDispatcher("/error.jsp");
        rd.forward(request, response);
        e.printStackTrace();

    }

    finally {
        request.setAttribute("UserData", lst);
        RequestDispatcher rd=request.getRequestDispatcher("/displayrecord.jsp");
        rd.forward(request, response);
        lst.clear();
        out.close();
    }
}

}

我不知道如何过滤结果集并显示已过滤的记录。任何人都可以指导我

0 个答案:

没有答案