我正在创建一个程序,将数据从数据库输出到饼图和表格中。我正在"尝试"使用模型视图控制器设计。我的问题是我正在制作的结果集将有多个我需要提取的值,但我不知道如何存储它们。我目前的代码是:
PreparedStatement returnDealerData = con.prepareStatement
("SELECT * FROM project.standard_dealer_link WHERE DealerID = "+ this.DealerID +
"and Quarter =" + this.Quarter + "and Year ="+ this.Year + ";");
ResultSet dealerRS = returnDealerData.executeQuery();
while (dealerRS.next()) {
this.DealerID = dealerRS.getInt("DealerID");
this.StandardID = dealerRS.getInt("StandardID");
this.Achieved = dealerRS.getString("Achieved");
this.NSCWaiver = dealerRS.getString("NSC_Waiver");
this.Override = dealerRS.getString("Override");
this.Quarter = dealerRS.getInt("Quarter");
this.Year = dealerRS.getInt("Year");
}
更改的数据将从标准 ID 到覆盖。所以我需要做的是,对于每次迭代,我需要以某种方式存储值,以便我可以将其放入饼图和表中。我认为让我感到困惑的是,我将此方法用于仅包含一组值的结果集。任何帮助,非常感谢:)。
请迭代值并将每一行存储到单独的数据对象中。因此,我们需要构造对象列表。
List<DataObject> list = new ArrayList<DataObject>();
while (dealerRS.next()) {
DataObject obj = new DataObject();
obj.setDealerID(dealerRS.getInt("DealerID"));
obj.setStandardID(dealerRS.getInt("StandardID"));
obj.setAchieved(dealerRS.getString("Achieved"));
obj.setNSCWaiver(dealerRS.getString("NSC_Waiver"));
obj.setOverride(dealerRS.getString("Override"));
obj.setQuarter(dealerRS.getInt("Quarter"));
obj.setYear(dealerRS.getInt("Year"));
list.add(obj);
}