JSP搜索数据库

时间:2013-12-03 17:30:21

标签: java mysql sql jsp search

场景1:用户想要找到管理员,他的名字是jan。他输入'admin jan'或'jan admin'。然后结果应该是:如果数据库中有'jan'和'admin'的用户,它将显示该用户。即使有10个人在他们的电子邮件,街道名称或其他内容中使用'jan'和'admin',它也应该显示出来。 场景2:用户希望找到具有'HQL PHP bebe SQL JAVA'技能的名为'bebe'的人。它应该在数据库中向每个用户显示这些单词。因此它应该只显示具有这些技能和名称的用户,所以如果其他人没有技能HQL,但确实具有PHP SQL JAVA技能,请不要显示这一点。

我只搜索最后一个字。因此,如果我搜索“这就是PHP SQL ADMIN”,它只会找到所有拥有ADMIN的人并忽略其余的字符串。

if (action.equals("searchUser")) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String searchQuery = request.getParameter("searchQuery");
    String[] query = searchQuery.split(" ");

    for (int i = 0; i < query.length; i++) {
        String hql = "FROM User WHERE username LIKE '%" + query[i] + "%' OR firstname LIKE '%" + query[i] + "%' OR lastname LIKE '%" + query[i] + "%' OR emailAddress LIKE '%" + query[i] + "%'";

        List<User> result = session.createQuery(hql).list();
        //set our results on the request and redirect back
        request.setAttribute("users", result);
        request.setAttribute("usersSize", result.size());
        request.setAttribute("usersSizeResults", result.size());

        System.out.println("size:" + result.size());
    }
    redirect(request, response, "/search.jsp");
    session.close();

}

2 个答案:

答案 0 :(得分:1)

HQL对使用的java类属性区分大小写。用户名和名字是否在User类中完全声明为?

可能是:

"FROM User WHERE (userName LIKE '%jan%' OR firstName LIKE '%jan%' OR lastName LIKE '%jan%' OR emailAddress LIKE '%jan%')"

小心你的foreach循环:你对列表的每个参数进行查询。并且您只发送响应中的最后一个结果。是对的吗??

可能是:

    if (action.equals("searchUser")) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String searchQuery = request.getParameter("searchQuery");
        String[] query = searchQuery.split(" ");

        List<User> usersFound = new ArrayList<User>();
        for (int i = 0; i < query.length; i++) {
            String hql = "FROM User WHERE username LIKE '%" + query[i] + "%' OR firstname LIKE '%" + query[i]
                    + "%' OR lastname LIKE '%" + query[i] + "%' OR emailAddress LIKE '%" + query[i] + "%'";
            List<User> result = session.createQuery(hql).list();
            if (result != null) {
                usersFound.addAll(result);
                System.out.println("size:" + result.size());
            }
        }

        // set our results on the request and redirect back
        request.setAttribute("users", usersFound);
        request.setAttribute("usersSize", usersFound.size());
        request.setAttribute("usersSizeResults", usersFound.size());

        redirect(request, response, "/search.jsp");
        session.close();
    }

答案 1 :(得分:1)

尝试此操作以避免重复值( NOT TESTED ):

public void searchUser() {
if (action.equals("searchUser")) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    String searchQuery = request.getParameter("searchQuery");
    String[] params = searchQuery.split(" ");

    // Found users
    List<User> usersFound = new ArrayList<User>();

    // Exact match
    String hqlMatch = this.getSearchHqlQuery(params, "AND");
    List<User> exactResult = session.createQuery(hqlMatch).list();
    if (exactResult!=null && !exactResult.isEmpty()) {
        usersFound.addAll(exactResult);
    }
    // Multiple search
    else {
        String hqlLike = this.getSearchHqlQuery(params, "OR");
        List<User> likeResult = session.createQuery(hqlLike).list();
        if (likeResult!=null && !likeResult.isEmpty()) {
            usersFound.addAll(likeResult);
        }
    }

    System.out.println("size:" + usersFound.size());
        // set our results on the request and redirect back
        request.setAttribute("users", usersFound);
        request.setAttribute("usersSize", usersFound.size());
        request.setAttribute("usersSizeResults", usersFound.size());

        redirect(request, response, "/search.jsp");
        session.close();
    }
}

private String getSearchHqlQuery(String[] params, String andOrfilter) {
    StringBuilder hql = new StringBuilder();
    hql.append("from User ");
    if (params.length > 0) {
        hql.append("where ");
        for (int i = 0; i < params.length; i++) {
            if (i > 0) { hql.append(andOrfilter); }
            hql.append(" (username like '%").append(params[i]);
            hql.append("%' OR firstname like '%").append(params[i]);
            hql.append("%' OR lastname like '%").append(params[i]);
            hql.append("%' OR emailAddress like '%").append(params[i]);
            hql.append("%') ");
        }
    }
    return hql.toString();
}