2012-08-10 132 views
1

以下SQL總是返回oneSELECT FOUND_ROWS()SELECT FOUND_ROWS()總是返回ONE,如何解決這個

我按照從網上這個例子:

public List<Employee> viewAllEmployees(
       int offset, 
       int noOfRecords) 
    { 
     String query = "select SQL_CALC_FOUND_ROWS * from employee limit " 
       + offset + ", " + noOfRecords; 
     List<Employee> list = new ArrayList<Employee>(); 
     Employee employee = null; 
     try { 
      connection = getConnection(); 
      stmt = connection.createStatement(); 
      ResultSet rs = stmt.executeQuery(query); 
      while (rs.next()) { 
       employee = new Employee(); 
       employee.setEmployeeId(rs.getInt("emp_id")); 
       employee.setEmployeeName(rs.getString("emp_name")); 
       employee.setSalary(rs.getDouble("salary")); 
       employee.setDeptName(rs.getString("dept_name")); 
       list.add(employee); 
      } 
      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 list; 
    } 

,這是基於上述我的代碼:

public List<TempcardViewModel> getTempcardHistory(String query) { 
     TempcardViewModel tempcardObj = null; 
     List<TempcardViewModel> tempcardList = new ArrayList<TempcardViewModel>(); 
     Connection connection = getConnection(); 
     if (connection != null) { 
      try { 
       PreparedStatement assingedTempcardPS = connection.prepareStatement(query); 
       ResultSet assingedTempcardlist_rst = assingedTempcardPS.executeQuery(); 
        while (assingedTempcardlist_rst.next()) { 
         tempcardObj = new TempcardViewModel(); 
         tempcardObj.setEmpid(assingedTempcardlist_rst.getInt("empid")); 
         tempcardObj.setEmpname(assingedTempcardlist_rst.getString("empname")); 
         tempcardObj.setTempcardnumber(assingedTempcardlist_rst.getString("tempcardnumber")); 
         tempcardObj.setIssuedate(assingedTempcardlist_rst.getString("issuedate")); 
         tempcardObj.setTempcardstatus(assingedTempcardlist_rst.getString("tempcardstatus")); 
         tempcardList.add(tempcardObj); 
        } 
        assingedTempcardPS.close(); 
        assingedTempcardlist_rst.close(); 

        PreparedStatement noOfRecordsPS = connection.prepareStatement("SELECT FOUND_ROWS();"); 
        assingedTempcardlist_rst = noOfRecordsPS.executeQuery(); 
        if(assingedTempcardlist_rst.next()) 
         this.noOfRecords = assingedTempcardlist_rst.getInt(1); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } finally { 
       try { 
        closeConnection(connection, null, null); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
     return tempcardList; 
    } 

我正在使用它進行分頁,這是我在jsp/servlets中完成的。

當我使用這個SELECT FOUND_ROWS()它在同一時間返回1

當我使用這個查詢:SELECT COUNT(empid) FROM acct_tempcardhistory;

它給人希望的放出來,但是當我去過濾比值是不正確的。

如何解決這個問題,請幫幫我。

感謝和問候

+1

有關的? http://stackoverflow.com/questions/1020745/found-rows-keeps-returning-0 – 2012-08-10 06:25:25

回答

1

您可以嘗試這樣;

PreparedStatement noOfRecordsPS = connection.prepareStatement("SELECT FOUND_ROWS() FROM acct_tempcardhistory;");