I have a query which has 4 Left Joins over 4 different tables. When I run this query in the workbench with a count() it returns 2 which is the expected value. The same query when running through my code returns 3000 i.e. count() as rowCount has a value of 3000. Has anyone here ran into similar problems?
Debug steps -
- On the debug mode I copied the query that was constructed in the Preparedstatement and ran it in the MySQL workbench. It's returning 2 which is correct.
Any suggestions on other ways that I can debug this issue?
Because of confidentiality purposes i cannot paste the query here
Edit :
int totalCount = 0;
StringBuilder queryBuilder = new StringBuilder(query.replace(QueryConstants.SELECT, QueryConstants.SELECT_COUNT));
try(Connection connection = dataSource.getConnection();
PreparedStatement countStatement = connection.prepareStatement(queryBuilder.toString());
ResultSet resultSet = countStatement.executeQuery()) {
while(resultSet.next()) {
totalCount = resultSet.getInt(1);
}
pageDetailsDTO.setTotal(totalCount);
So what I am trying to achieve here is, first I will execute a preparedStatement with LIMIT and OFFSET and get the result set. Secondly, passing the same preparedStatement as a String I will call the above logic inside a method called getTotalCount(String query, PageDetailsDTO pageDetailsDTO) to know the number of records that were returned without the LIMIT and OFFSET. This total count is for pagination purposes. So I will be replacing the Select with Select count() as rowCount*.