我解決了這個問題的另一種方式(通過更改設置通過而不是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));
}
}
嗯,我可以修改我的SQL,並使用「按名稱組」,我會得到類似的結果。我真正需要的是將產品轉移到一個大字符串中,列出該字段中的所有產品。 –