Hi, I'm trying to run a Gantt Chart, I tried to put some values in it, this is the code I'm trying to get running:
public class ServletDemo1 extends HttpServlet {
public ServletDemo1() {}
public void destroy(){
System.out.println("Servlet Destroyed");
}
IllegalAccessException{
GanttSeriesCollection collection = new GanttSeriesCollection();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String url = "jdbc:odbc:times";
Connection connect = DriverManager.getConnection(url,"hernanf4","kool24ps");
ResultSet rs;
Statement stmt = connect.createStatement();
String strQry = "SELECT tistiact.act_desc, tistiact.actual_start, tistiact.actual_finish";
strQry = strQry + ", tistiact.orig_late_start, tistiact.orig_late_finish";
strQry = strQry + " from tistiact where tistiact.order_no = 'F3654G1'";
rs = stmt.executeQuery(strQry);
System.out.println(strQry);
GanttSeries s1 = new GanttSeries("Closed");
GanttSeries s2 = new GanttSeries("Active");
String task;
java.sql.Date act_strt, act_fin, org_strt, org_fin;
while (rs.next()){
task = rs.getString(1);
act_strt = rs.getDate(2);
act_fin = rs.getDate(3);
org_strt = rs.getDate(4);
org_fin = rs.getDate(5);
if ((act_strt!= null)&&(act_fin!=null)){
s1.add(task, new TimeAllocation(date(act_strt), date(act_fin)));
collection.add(s1); }
if ((org_strt!= null)&&(org_fin!=null)) {
s2.add(task, new TimeAllocation(date(org_strt) ,date(org_fin)));
collection.add(s2);}
}
rs.close();
stmt.close();
connect.close();
return collection;
}
catch (SQLException e){
System.out.println(e.getMessage());
System.out.println(e.toString());
}
catch (NullPointerException e){
System.out.println(e.getMessage());
System.out.println(e.toString());
}
catch (Exception e){
e.printStackTrace();
System.out.println(e.toString());
}
return collection;
}
private static Date date(java.sql.Date dateStr) {
int year, month, day;
String dateString = dateStr.toString();
Date result = dateStr;
year = Integer.parseInt(dateString.substring(0,4));
month = Integer.parseInt(dateString.substring(5,7));
day = Integer.parseInt(dateString.substring(8,10));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
result = calendar.getTime();
return result;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
try {
IntervalCategoryDataset dataset = createGanttDataset();
JFreeChart chart = ChartFactory.createGanttChart("Activities for F3319G1", "Activity","Date", dataset, true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
}
catch (Exception e) {
System.err.println(e.toString());
}
finally {
out.close();
}
}
I just modified some of the code in the GanttDemo.java and the DemoSetDataFactory.java code... and I discovered that the statement JFreeChart = JFreeChart chart = ChartFactory.createGanttChart("Activities for F3319G1", "Activity","Date", dataset, true); it's causing the problem I get this run-time error:
java.lang.IndexOutOfBoundsException: Index -1:, Size: 16, this 16 number is the number of records is the ResultSet, could you please help me with this?, thanks in advance.
Sergio.
IndexOutOfBoundsException when creating chart
Re: IndexOutOfBoundsException when creating chart
Sorry the definition of the function createGanttChart is:
public static IntervalCategoryDataset createGanttDataset() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
Thanks again.
public static IntervalCategoryDataset createGanttDataset() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
Thanks again.