【问题标题】:SELECT FOUND_ROWS() always returns ONE, How to solve thisSELECT FOUND_ROWS() 总是返回 ONE,如何解决这个问题
【发布时间】:2012-08-10 16:27:28
【问题描述】:

下面的sql总是返回oneSELECT 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;

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

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

感谢和问候

【问题讨论】:

标签: java jsp servlets pagination


【解决方案1】:

你可以这样试试;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2021-07-28
    • 2022-10-18
    相关资源
    最近更新 更多