我以另一种方式解决了这个问题(通过更改通过 SQL 结果集创建列表的方式)。不过,这仍然是一个有趣的问题,我同意我应该使用 HashMap。谢谢您的帮助。
这是代码,以防有人遇到类似问题:
conn = DriverManager.getConnection(url, userName, password);
stmt = conn.createStatement();
ResultSet rset = null;
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("select * FROM CourseProduct "
+ "INNER JOIN Course "
+ "ON CourseProduct.number=Course.number "
+ "inner join Product "
+ "on CourseProduct.P_ID=Product.P_ID "
+ "WHERE "
+ "(superProduct = ? or ? ='') "
+ "and (product = ? or ? ='') "
+ "and (location = ? or ? ='') "
+ "and (type = ? or ? ='') "
+ "and (category = ? or ? ='') "
+ "order by Course.number asc");
pstmt.setString(1, superProduct);
pstmt.setString(2, superProduct);
pstmt.setString(3, product);
pstmt.setString(4, product);
pstmt.setString(5, location);
pstmt.setString(6, location);
pstmt.setString(7, type);
pstmt.setString(8, type);
pstmt.setString(9, category);
pstmt.setString(10, category);
rset = pstmt.executeQuery();
String tempString = " ";
String tempNumber1=null, tempNumber2 = null;
while (rset.next()) {
// here we go
tempNumber1 = rset.getString("number");
tempString+=", "+rset.getString("product");
if (tempNumber1 == null ? tempNumber2 == null : tempNumber1.equals(tempNumber2)) {
courseListing.remove(courseListing.size()-1);
courseListing.add(new CourseListing(
rset.getDouble("price"),
rset.getString("number"),
rset.getString("name"),
rset.getString("location"),
rset.getString("unit"),
rset.getString("duration"),
rset.getString("type"),
rset.getString("role"),
rset.getString("category"),
rset.getString("maxNumStudents"),
rset.getString("superProduct"),
rset.getString("subProduct"),
tempString));
}
else {
tempString=rset.getString("product");
tempNumber2 = rset.getString("number");
courseListing.add(new CourseListing(
rset.getDouble("price"),
tempNumber2,
rset.getString("name"),
rset.getString("location"),
rset.getString("unit"),
rset.getString("duration"),
rset.getString("type"),
rset.getString("role"),
rset.getString("category"),
rset.getString("maxNumStudents"),
rset.getString("superProduct"),
rset.getString("subProduct"),
tempString));
}
}