好的,如果我打印出循环内的值,我将得到db的所有结果,但是当我尝试循环思考Driver类中的数组列表时,我只得到数组的最后一个值。
/ ************************** studentdata班********************* ********* /
public ArrayList<Student> AllStudentdata() throws SQLException {
Student student = null;
ArrayList<Student> Studentlist = new ArrayList<Student>();
String Sql = "CALL `studentsdb`.`SelectStudentsGarde`()";
try {
stat = con.createStatement();
result = stat.executeQuery(Sql);
while (result.next()) {
student = new Student();
student.setFristName(result.getString(1));
student.setLastName(result.getString(2));
Studentlist.add(student);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return Studentlist;
}
/ **************************司机班****************** ************ /
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> Studentlist=new ArrayList<Student>();
Studentdata studata = new Studentdata();
try {
Studentlist=studata.AllStudentdata();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
System.out.println(Studentlist.size());
for(int i = 0;i<Studentlist.size();i++)
{
System.out.println(Studentlist.get(i));
}
}
答案 0 :(得分:3)
在黑暗中拍摄,我没有调试这个,但你继续在你的循环中重置学生参考。试试这个:
thingObj