class DBQueries {

    public static <O> int count(final Query<O> query, final String tableName, final Connection connection) {

        final String selectSql = String.format("SELECT COUNT(objectKey) FROM cqtbl_%s", tableName);
        PreparedStatement statement = null;
        try {
            statement = createAndBindSelectPreparedStatement(selectSql, "", Collections.<WhereClause>emptyList(), query, connection);
            java.sql.ResultSet resultSet = statement.executeQuery();

            if (!resultSet.next()) {
                throw new IllegalStateException("Unable to execute count. The ResultSet returned no row. Query: " + query);
            }

            return resultSet.getInt(1);
        }
        catch (Exception e) {
            throw new IllegalStateException("Unable to execute count. Query: " + query, e);
        }
        finally {
            DBUtils.closeQuietly(statement);
        }
    }

}