Search code examples
javaswingjdbcresultset

list elements not accumulating properly


SchoolYear model

 public class SchoolYear {
    int id;
    int start;
    int end;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}

SchoolYearDaoImpl class method

public List<SchoolYear> getAllSchoolYearStart() {
        String SQL = "{CALL getAllSchoolYearInfo()}";
        SchoolYear schoolyear = new SchoolYear();
        List<SchoolYear> list = new ArrayList<>();
        try(Connection con = DBUtil.getConnection(DBType.MYSQL);
                CallableStatement cs = con.prepareCall(SQL);){
            try(ResultSet rs = cs.executeQuery();){
                while(rs.next()){
                    schoolyear.setStart(rs.getInt("yearFrom"));
                    list.add(schoolyear);
                }

                JOptionPane.showMessageDialog(null,list.size());
                for(int x=0; x<list.size(); x++){
                    JOptionPane.showMessageDialog(null,((SchoolYear)list.get(x)).getStart());
                }
            }
        }catch(SQLException e){
            JOptionPane.showMessageDialog(null,e.getMessage());
        }
        return list;
    }

The result set I get by calling my stored procedure getAllSchoolYearInfo() shows 2 rows for yearFrom column.

enter image description here

However, when I ran the program to see what's on my GUI, I get 2 values in my JComboBox which appears to be the row 2's value 2015

enter image description here

As you can see I tried to get the size of the list to see how many objects are stored. So I'm guessing it could be a problem with how objects are stored and accumulated to the List<SchoolYear> in this block of code.

try(ResultSet rs = cs.executeQuery();){
                while(rs.next()){
                    schoolyear.setStart(rs.getInt("yearFrom"));
                    list.add(schoolyear);
                }

                JOptionPane.showMessageDialog(null,list.size());
                for(int x=0; x<list.size(); x++){
                    JOptionPane.showMessageDialog(null,((SchoolYear)list.get(x)).getStart());
                }
            }

Or could the problem be with my ListCellRenderer? I don't think it is.

jcmbSchoolYearStart.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof SchoolYear){
                    SchoolYear schoolyear = (SchoolYear) value;
                    setText(""+schoolyear.getStart());
                }
                return this;
            }
        } );

Any thoughts?

Thanks.


Solution

  • You are only creating one SchoolYear instance. Because you do that, you only have a single reference in your list. You could comment out your current SchoolYear schoolyear and move it into the loop (and fix everywhere else you're using it), or just declare a new local variable in the loop like.

    while(rs.next()){
        SchoolYear sy = new SchoolYear();
        sy.setStart(rs.getInt("yearFrom"));
        list.add(sy);
    }