Appear to be missing the final row in the resultset when using while(rs.next())

151
April 26, 2019, at 9:50 PM

I seem to be missing out the final row of the result set, I have entries for monday, tuesday, wednesday and friday in the db and only the friday one is not being outputted, how do I resolve this? Im assuming this is probably to do with the iteration over the resultset (while(rs.next()))?

public ArrayList<ArrayList<Activity>> getActivities()
    {
        ArrayList<ArrayList<Activity>> activitiesByDay = new ArrayList<>(); 
        activitiesByDay.add(new ArrayList<>());
        activitiesByDay.add(new ArrayList<>());
        activitiesByDay.add(new ArrayList<>());
        activitiesByDay.add(new ArrayList<>());
        activitiesByDay.add(new ArrayList<>());
        ArrayList<Activity> allActivities = new ArrayList<>();
        Connection connection;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, this.password);
            System.out.println("Database connected!");      
            PreparedStatement ps = connection.prepareStatement("SELECT `activity`.`ActivityID`,`activity`.`ActivityName`,`activity`.`StartDate`,`activity`.`EndDate`,`activity`.`ActivityType`,`activity`.`Capacity`,`activity`.`YearGroups`,`activity`.`Description` FROM `activity` ORDER BY `activity`.`StartDate` ASC, `activity`.`EndDate` ASC,`activity`.`ActivityName` ASC;");//Return activities available to the students yeargroup in order of startdate and activityname
            System.out.println(ps);
            ResultSet rs = ps.executeQuery();
            while(rs.next())//Get all the activities available to the student's yeargroup.
            {
                Activity temp = new Activity();
                temp.setActivityID(rs.getInt(1));
                temp.setActivityName(rs.getString(2));
                temp.setStartDate(Dates.valueOf(rs.getString(3)));
                temp.setEndDate(Dates.valueOf(rs.getString(4)));
                temp.setActivityType(ActivityTypes.valueOf(rs.getString(5)));
                temp.setCapacity(rs.getInt(6));
                temp.setYearGroups(stringToIntArray(rs.getString(7)));
                temp.setDescription(rs.getString(8));
                allActivities.add(temp);
            }
            connection.close();
        } catch (SQLException | ClassNotFoundException ex) {
            System.out.println(ex);
        }
        for(Activity activity : allActivities)
        {
            if(activity.getStartDate()==Dates.MONDAY){
                activitiesByDay.get(0).add(activity);
            }
            if(activity.getStartDate()==Dates.TUESDAY){
                activitiesByDay.get(1).add(activity);
            }
            if(activity.getStartDate()==Dates.WEDNESDAY){
                activitiesByDay.get(2).add(activity);
            }
            if(activity.getStartDate()==Dates.THURSDAY){
                activitiesByDay.get(3).add(activity);
            }
            if(activity.getStartDate()==Dates.FRIDAY){
                activitiesByDay.get(4).add(activity);
            }
        }
        return activitiesByDay;
    }

Edit: 3rd pic is the output after the end of getActivities, it seems to have added the Friday activity, so presumably the error is in the servlet? This is the part in the servlet where the arraylist of all activities is iterated over:

for(ArrayList<Activity> array: allActivities)
            {
                out.println("<hr>");
                out.println("<h3>"+capitaliseFirstLetterOnly(array.get(0).getStartDate().toString())+"</h3></br>");
                for(Activity activity: array)
                {
                    out.println("<b>"+activity.getActivityName()+"</b>");
                    out.println("<form action = \"teachereditactivitypage\" method=\"post\">"
                    + "<table width='100%' border='1'>"
                    + "<tr>"
                    + "<td>Activity Type</td>"
                    + "<td>"+activity.getActivityType().toString()+"</td>"
                    + "</tr>"
                    + "<tr>"
                    + "<td>Capacity</td>"
                    + "<td>"+activity.getCapacity()+"</td>"
                    + "</tr>"
                    + "<tr>"
                    + "<td>Year Groups</td>"
                    + "<td>"+Arrays.toString(activity.getYearGroups())+"</td>"
                    + "</tr>");
                    if(activity.getActivityType()==Activity.ActivityTypes.PROJECT)
                    {
                        out.println("<tr>"
                        + "<td>Day</td>"
                        + "<td>"+activity.getStartDate()+" and "+activity.getEndDate()+"</td>"
                        + "</tr>");
                    }
                    else{
                        out.println("<tr>"
                        + "<td>Day</td>"
                        + "<td>"+activity.getStartDate()+"</td>"
                        + "</tr>");
                    }
                    out.println("<tr>"
                    + "<td>Description</td>"
                    + "<td>"+activity.getDescription()+"</td>"
                    + "</tr>"
                    + "</table>"
                    + "<input type='hidden' name='ActivityID' value="+activity.getActivityID()+">"
                    + "<input type='submit' value='Edit'></br></br>"
                    + "<input type='submit' formaction=\"teacherremoveactivity\" value='Remove'>"
                    + "</form>"
                    + "</br>");
                }
            }

Edit: When the query is ran in the db, it outputs correctly

Edit: Dates enum

public enum Dates{
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY
    };
Rent Charter Buses Company
READ ALSO
Determining Whether Graph G = (V,E) Contains a Negative-Weight Cycle

Determining Whether Graph G = (V,E) Contains a Negative-Weight Cycle

In this program, I am given an input text file that gives information about a weighted, directed graph

129
How to convert bytes to mp3 and how to send data over UDP

How to convert bytes to mp3 and how to send data over UDP

1) I am able to input and send my file name from my client to my serverbut once it reaches my server I am unable to access the file even though the location and name of the file is correct

216
What tests are causing my codingbat answer to String-3, &ldquo;SameEnds&rdquo; to fail?

What tests are causing my codingbat answer to String-3, “SameEnds” to fail?

I'm working on the SameEnds problem in String-3 on codingbat and I can not understand why it keeps failing the "other tests" sectionEvery other test works

131
Lookup map in multi thread env in java

Lookup map in multi thread env in java

I am thinking of have a lookup map that can be used for a value check

141