首先看下面一段代码:
ArrayList<Person> people = new ArrayList<>();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
Statement statement1 = null;
ResultSet resultSet = null;
Person person = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://116.196.124.159/shischcool", "root", "root");
statement1 = connection.createStatement();
String sql = "select * from student3 ";
resultSet = statement1.executeQuery(sql);
person = new Person();
int anInt = 0;
String str ;
while(resultSet.next())
{
anInt = resultSet.getInt(1);
person.setId(anInt);
anInt = resultSet.getInt(3);
person.setAge(anInt);
anInt = resultSet.getInt(6);
person.setMath(anInt);
anInt = resultSet.getInt(7);
person.setEnglish(anInt);
str = resultSet.getString("name");
person.setName(str);
str = resultSet.getString("sex");
person.setSex(str);
str = resultSet.getString("address");
person.setAddress(str);
System.out.println(person);
people.add(person);
}
System.out.println(people);
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(statement1 != null) {
try {
statement1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
打印的结果是:
[Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}, Person{Id=8, Name='德玛西亚', Age=18, Sex='男', Address='南京', Math=56, English=65}]
看到这个结果我就不乐意了,我明明是想存入8个不一样的Person ,并且我在list的add 之前修改了person的属性值,为什么呢?
原来,list 存储的是引用,
虽然你每次都修改了属性值,并且重新添加到了list里面,但是对于最后一次的操作才是最后的赋值操作,前面的操作都是作废的,因为list去遍历的时候是去找找个引用的地址里面的值,所以这样list里面的引用值是一样的,那值肯定都是最后一次赋值的值咯,
解决方法:
person 变量可以只需要定义一次,但是可以new多次,对应不用的引用地址,这样就不会出现每次的值都是一样的
ArrayList<Person> people = new ArrayList<>();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
Statement statement1 = null;
ResultSet resultSet = null;
Person person = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://116.196.124.159/shischcool", "root", "root");
statement1 = connection.createStatement();
String sql = "select * from student3 ";
resultSet = statement1.executeQuery(sql);
int anInt = 0;
String str ;
while(resultSet.next())
{
person = new Person();
anInt = resultSet.getInt(1);
person.setId(anInt);
anInt = resultSet.getInt(3);
person.setAge(anInt);
anInt = resultSet.getInt(6);
person.setMath(anInt);
anInt = resultSet.getInt(7);
person.setEnglish(anInt);
str = resultSet.getString("name");
person.setName(str);
str = resultSet.getString("sex");
person.setSex(str);
str = resultSet.getString("address");
person.setAddress(str);
System.out.println(person);
people.add(person);
}
System.out.println(people);
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(statement1 != null) {
try {
statement1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
![](/qrcode.jpg)