将ResultSet结果集转换为List,打印出来的结果却全是第三条记录。
这是我的数据库表

这是结果



@Test
public void TestSelectAll() throws Exception {
//用连接池的方法
//1. 导入Druid jar包
//2. 定义配置文件
//3. 加载配置文件
Properties prop = new Properties() ;
prop.load(new FileInputStream("src/durid.properties"));
//4. 获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接
Connection conn = dataSource.getConnection();
//6.定义SQL语句
String sql = "select * from brand1";
//7.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//8.设置参数
//9.执行SQL
ResultSet res = pstmt.executeQuery();
//10.处理结果:
// 先将查询到结果封装成Brand对象,再装载到List集合里
Brand bra= new Brand();
List<Brand> brands = new ArrayList<Brand>();
while (res.next()){
//获取数据
int id = res.getInt("id");
String brandName = res.getString("brand_name");
String companyName = res.getString("company_name");
int ordered = res.getInt("ordered");
String description = res.getString("description");
int status = res.getInt("status");
//封装Brand对象
bra.setId(id);
bra.setBrandName(brandName);
bra.setCompanyName(companyName);
bra.setOrdered(ordered);
bra.setDescription(description);
bra.setStatus(status);
//装载集合
brands.add(bra);
}
System.out.println(brands);
//11.释放资源
res.close();
pstmt.close();
conn.close();
}