次の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/サーブレットで行ったページングにこれを使用しています。
これを使用するSELECT FOUND_ROWS()と返されます1
このクエリを使用すると同時に:SELECT COUNT(empid) FROM acct_tempcardhistory;
必要な出力が得られますが、フィルタリングを行うと、値が正しくありません。
これを解決する方法、私を助けてください。
感謝とよろしく