1

以下 sql 总是返回one SELECT FOUND_ROWS()

我从网上遵循了这个例子:

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;

它提供了所需的输出,但是当我进行过滤时,值不正确。

如何解决这个问题,请帮助我。

谢谢并恭祝安康

4

1 回答 1

1

你可以这样试试;

   PreparedStatement noOfRecordsPS = connection.prepareStatement("SELECT FOUND_ROWS() FROM acct_tempcardhistory;");
于 2012-08-10T08:27:28.937 回答