2012-11-28 38 views
0

在我遇到了一些問題的動態Web應用程序獲取空值,所以我張貼我的代碼:從屬性

這裏是我的控制器(servlet的):

 String select = request.getParameter("select"); // getting proper value 
     String search = request.getParameter("search"); // getting proper 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> list=searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search); 
      int noOfRecords = searchDAO.getNoOfRecords(); 
        System.out.println("4> NoOfRecords : "+noOfRecords); 
      int noOfPages = (int) Math.ceil(noOfRecords * 1.0/recordsPerPage); 
        System.out.println("5> NoOfPages : "+noOfPages); 
      request.setAttribute("searchList", list); 
      System.out.println("6> List : "+list); 
      request.setAttribute("noOfPages", noOfPages); // Getting null value, 0 
      request.setAttribute("currentPage", page); // Getting null value, 0 
      RequestDispatcher view = request.getRequestDispatcher("/jsp/Securex_Anti_Theft_SearchUserList.jsp"); 
      view.forward(request, response); 

這裏是我的DAO (簡單的Java類):

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); 
     System.out.println("1> :"+rs); 
     while(rs.next()){ 
     user1=new User(); 
     user1.setSerial(rs.getInt(1)); 
     System.out.println("I'm inside a loop"); 
     user1.setName(rs.getString(2)); 
     user1.setEmail(rs.getString(3)); 
     list1.add(user1); 
     } 

     rs.close(); 
     rs = stmt.executeQuery("SELECT FOUND_ROWS()"); 
     System.out.println("2> :" +rs); 
     if(rs.next()) 
      this.noOfRecords = rs.getInt(1); 
      System.out.println("3> :" +this.noOfRecords); 

    } 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; 
} 
} 

並且輸出是:

1> :[email protected] 
2> :[email protected] 
3> :0 
4> NoOfRecords : 0 
5> NoOfPages : 0 
6> List : [] 

我有相同的servlet和class來選擇所有用戶,並且工作正常,但是從這裏獲得空值。即使ResultSet返回值,即com.mysql.jdbc.JDBC4ResultSet @ 88d319,但無法將值從DAO讀取到servlet 。

//屬性獲取空值,對我來說一切都應該去罰款,但仍然得到空值,隨意地指出我的錯誤,如果我最有可能你的DAO返回空單做了什麼

+0

你確定,你的DAO返回不是空的結果? – udalmik

+0

ResultSet返回值,即com.mysql.jdbc.JDBC4ResultSet @ 88d319,但是無法從DAO獲取值到您的Dao類中的servlet –

+0

,爲什麼searchAllUsers和getConnection方法是嵌套的? – Kent

回答

1

。所有其他代碼看起來不錯。所以,首先我會檢查這個調用

searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search); 

什麼是page變量的值?其他參數?請登錄後查詢

String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+ 
"' like '%"+search+"%' order by serialNo asc limit 
" + offset + " , " + noOfRecords; 

,並分別對數據庫運行它,它應該給你一個答案。

+0

我已經在mysql控制檯中運行了它數次,它給出了正確的答案,並且我已經更新了我的Ques。 :( –

+0

由於頁面等價於偏移量,所以我將它賦值爲1,所以極限值變爲0,noOfRecords。 –

+0

我發現rs.next不會進入while循環....任何原因。 –