我无法从数据库中获取绘制图表的任务的开始日期和结束日期
public IntervalCategoryDataset getCategoryDataset() {
conn = ConnectDB.ConnectDB();
TaskSeriesCollection dataset = new TaskSeriesCollection();
String sql = "SELECT `TITRE`, `DATE DEBUT Prévi`, `DATE FIN prévi` FROM `projet`;";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
while (rs.next()) {
String a = rs.getString("TITRE");
Date StartDate = rs.getDate("DATE DEBUT Prévi");
Date EndDate = rs.getDate("DATE FIN prévi");
Names.add(a);
Dates.add(StartDate);
Dates.add(EndDate);
++count;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
int j = 0;
int k = 1;
TaskSeries series1 = new TaskSeries("Estimated Date");
for (int i = 0; i < count; i++) {
series1.add(new Task(Names.get(i),
Date.from(LocalDate.of(Dates.get(j).getYear(), Dates.get(j).getMonth(), Dates.get(j).getDay())
.atStartOfDay()
.toInstant(ZoneOffset.UTC)),
Date.from(LocalDate.of(Dates.get(k).getYear(), Dates.get(k).getMonth(), Dates.get(k).getDay())
.atStartOfDay()
.toInstant(ZoneOffset.UTC))));
}
dataset.add(series1);
return dataset;
}
我希望每个任务都有其图表,但是所有任务都有相同的图表。
答案 0 :(得分:1)
您应该应用一个循环,此外,请使用getObject(index, LocalDate.class)
或-如果驱动程序不支持先前的选项,请使用-java.sql.Date.toLocalDate()
:
public IntervalCategoryDataset getCategoryDataset() {
conn = ConnectDB.ConnectDB();
TaskSeriesCollection dataset = new TaskSeriesCollection();
TaskSeries series1 = new TaskSeries("Estimated Date");
String sql = "SELECT `TITRE`, `DATE DEBUT Prévi`, `DATE FIN prévi` FROM `projet`;";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("TITRE");
LocalDate startDate = rs.getObject("DATE DEBUT Prévi", LocalDate.class);
LocalDate endDate = rs.getObject("DATE FIN prévi", LocalDate.class);
series1.add(new Task(name,
Date.from(startDate.atStartOfDay().toInstant(ZoneOffset.UTC)),
Date.from(endDate.atStartOfDay().toInstant(ZoneOffset.UTC)));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
dataset.add(series1);
return dataset;
}
答案 1 :(得分:0)
您总是会得到相同的结果,因为j
和k
值堆叠在它们的默认值中,并且不会与i
循环中的for
计数器一起递增。关于正在查看的代码,我建议您删除j
和k
计数器,将j
替换为i
,将k
替换为{ {1}}。
您的for循环应如下所示:
i+1