问题
I have problem in taking from the database the startdate and the finaldate of task to draw the chart
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;
}
I expect to have for every tasks its chart but I have the same chart for all the tasks.
回答1:
You should apply a single loop, and in addition either use getObject(index, LocalDate.class)
or - if your driver doesn't support the previous option - 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;
}
回答2:
You always get the same result because the j
and k
values are stacked in their default values and not incrementing along with the i
counter in your for
loop. Referring to the code am seeing, i advise you to get rid of the j
and k
counters, replace the j
with i
and the k
with i+1
.
Your for loop should be like the following:
for (int i =0; i<count; i++) {
series1.add(new Task(Names.get(i),
Date.from(LocalDate.of(Dates.get(i).getYear(), Dates.get(i).getMonth(),Dates.get(i).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC)),
Date.from(LocalDate.of(Dates.get(i+1).getYear(),Dates.get(i+1).getMonth(),Dates.get(i+1).getDay()).atStartOfDay().toInstant(ZoneOffset.UTC))
)
);
}
来源:https://stackoverflow.com/questions/56296842/how-can-i-draw-gantt-chart-with-date-from-database-in-java