得到Null值+可能查询不起作用 [英] getting Null value + probably query isn't working

查看:125
本文介绍了得到Null值+可能查询不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我正在遵循mvc-3层架构到我的应用程序。这是我的搜索选项的实现。
在我的servlet(控制器)中我这样做:

Actually I'm following mvc-3 tier architecture to my application.and here is my Search option's implementation. In my servlet(controller) I'm doing this :

        String select = request.getParameter("select");  // getting value
        String search = request.getParameter("search");  // getting value
        request.setAttribute("select", select);
        request.setAttribute("search", search);
        System.out.println("Select : "+select+" Search : "+search);

        int page = 1;
        int recordsPerPage = 20;
        if(request.getParameter("page") != null)
            page = Integer.parseInt(request.getParameter("page"));
        SearchDAO searchDAO=new SearchDAO();
        List<User> list1=searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search);  //getting null value,doing SOP of list1
        int noOfRecords = searchDAO.getNoOfRecords();  //getting null value,doing SOP
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);  //getting null value,doing SOP
        session.setAttribute("noOfRecords", noOfRecords);
        request.setAttribute("searchList", list1);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);

在SearchDAO中是:

and in the SearchDAO is:

public class SearchDAO {
private int noOfRecords;
Connection connection;
Statement stmt;

    public List<User> searchAllUsers(int offset ,int noOfRecords,String select,String search){
    private static Connection getConnection() throws SQLException,ClassNotFoundException{
    Connection con = ConnectionFactory.getInstance().getConnection();
    return con;  //ConnectionFactory is class for making the connection to DB.
}
     String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+
"' like '%"+search+"%' order by serialNo asc limit 
" + offset + " , " + noOfRecords;

    List<User> list1 = new ArrayList<User>();
    User user1=null;

    try {
        connection = getConnection();
        stmt = connection.createStatement();
        ResultSet rs=stmt.executeQuery(query);
        while(rs.next()){
        user1=new User();
        user1.setSerial(rs.getInt(1));
            user1.setName(rs.getString(2));
            user1.setEmail(rs.getString(3));
            list1.add(user1);
            }

        rs.close();
        rs = stmt.executeQuery("SELECT FOUND_ROWS()");
        if(rs.next())
            this.noOfRecords = rs.getInt(1); 

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally
    {
        try {
            if(stmt != null)
                stmt.close();
            if(connection != null)
                connection.close();
            } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return list1;

}

public int getNoOfRecords() {
    return noOfRecords;
}
}




  • 编辑
    随意评论&如果我没有遵循正确的mvc架构,请纠正我。

  • 推荐答案

    AFAIK,创建一个bean类,在类的帮助下,你可以在控制器和DAO之间进行通信,你不能通过使用request.getAttribute(在你的DAO中)来实现。

    AFAIK, create a bean class and with the help of the class, you can communicate between controller and DAO, you could not do it by using request.getAttribute(which is in your DAO).

    这篇关于得到Null值+可能查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆