使用数据访问对象&获取JFreeChart数据。 Servlet的

时间:2014-01-27 09:39:18

标签: database servlets jfreechart dao bar-chart

我的目标是使用Java生成条形图,以便搜索可以执行此操作的库。很快我找到了JFreeChart库并从这里下载了它。

给出的教程是数据是硬编码。所以作为我的榜样。但是,我正在尝试使用数据访问对象(DAO)和Servlet 从数据库中获取JFreeChart数据。

This example显示如何从数据库中获取JFreeChart数据。但它不使用DAO。

是否有人可以帮助我如何使用DAO和SERVLET获取JFreeChart数据?

在DAO中,我应该创建一个返回arraylist,整数,布尔值的方法吗?

我希望我的图表看起来像这样。 Bar Graph

我的数据库

id | revenue | year 
_______________________
R1 | 999     | 2012
R1 | 832     | 2013
R2 | 318     | 2012
R2 | 111     | 2013
R3 | 342     | 2012
R3 | 873     | 2013

我的servlet

public JFreeChart getChart() {


    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.setValue(999, "2012", "R1");
    dataset.setValue(832, "2013", "R1");
    dataset.setValue(318, "2012", "R2");
    dataset.setValue(111, "2013", "R2");
    dataset.setValue(342, "2012", "R3");
    dataset.setValue(873, "2013", "R3");

    JFreeChart chart = ChartFactory.createBarChart3D("",
            "Years", // domain axis label
            "Revenue", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false);

    CategoryPlot plot = chart.getCategoryPlot();
    CategoryAxis axis = plot.getDomainAxis();
    axis.setCategoryLabelPositions(CategoryLabelPositions
            .createUpRotationLabelPositions(Math.PI / 8.0));

    CategoryItemRenderer renderer = plot.getRenderer();
    BarRenderer r = (BarRenderer) renderer;
    r.setMaximumBarWidth(0.75);

    return chart;
}

1 个答案:

答案 0 :(得分:1)

要在servlet中进行简单访问,请创建一个类并将实例变量声明为数据库表

public class Revenue
{
   private String id;
   private Integer revenue;
   private String year;

   //getters and setters
}

现在,从数据库中获取数据,在DAO中创建一个方法并编写

public ArrayList<Revenue> getAllRevenueList()
{
  try
  {
       // try-catch exception, if any exception occurs return null
      //driver 
      //connection code 

     String sql = "SELECT id,revenue,year FROM revenue_table ORDER BY year";
     Statement stmt = con.createStatemtnt();
     ResultSet rs = stmt.executeQuery(sql);
     //now iterate over the result set and create list of objects of class `Revenue`

     //create an ArrayList<Revenue>
     ArrayList<Revenue> revenueList = new ArrayList<Revenue>();
     while(rs.next)
     {
        //for each row in result set create the object of class Revenue
        Revenue r = new Revenue();
        r.setId(rs.getString("id"));
        r.setRevenue(Integer.parseInt(rs.getString("revenue")));
        r.setYear(rs.getString("year"));
        revenueList.add(r);  //add object to list
     }

     return revenueList;
  }
  catch(Exception e)
  {
     e.printStackTrace();
     return null;
  }
}

的Servlet

public JFreeChart getChart() 
{
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

  //retrieve the revenue ArrayList
  //make sure that you have initialized the revenueDao object before calling method
  ArrayList<Revenue> revenueList = revenueDao.getAllRevenueList(); 
  //check list is not null and empty
  if(revenueList != null && !revenueList.isEmpty())
  {
     for(Revenue r : revenueList)
     {
        dataset.setValue(r.getRevenue(), r.getYear, r.getId());    
     }
  }

  ..........
  ............
  ..............
}