列表元素没有正确累积

时间:2017-02-03 03:38:21

标签: java swing jdbc resultset

SchoolYear 模型

 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 类方法

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;
    }

通过调用我的存储过程getAllSchoolYearInfo()得到的结果集显示了 yearFrom 列的2行。

enter image description here

然而,当我运行该程序以查看我的GUI上的内容时,我在JComboBox中获得了2个值,该值似乎是第2行的值 2015

enter image description here

正如您所看到的,我试图获取列表的大小以查看存储的对象数量。所以我猜这可能是对象如何存储和累积到这段代码中List<SchoolYear>的问题。

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());
                }
            }

问题可能出在ListCellRenderer上吗?我不这么认为。

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;
            }
        } );

有什么想法?

感谢。

1 个答案:

答案 0 :(得分:3)

您只创建一个SchoolYear实例。因为您这样做,所以list中只有一个引用。您可以注释掉当前的SchoolYear schoolyear并将其移动到循环中(并修复您正在使用它的其他任何地方),或者只是在循环中声明一个新的局部变量,如。

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