0

I write the code in fetch data from mysql table and create chart for that value and insert that chart into pdf and store it. but I got error. sql data fetched and pdf also created but not open and java give error. I use jfreechart-1.0.18,itextpdf-5.5.2,jcommon jar files. My java code is..

public static void main(String args[]) throws SQLException {
    System.out.println("Enter No");
    Scanner sc = new Scanner(System.in);
    String no = sc.next();
    writeChartToPDF(createChart(no), 550, 400, "test.pdf");
}

public static JFreeChart createChart(String no) throws SQLException {
    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    master ms = new master();
    String c1 = null, c2 = null, c3 = null, c4 = null;
    ms.conn();
    ResultSet rs;
    PreparedStatement ps;
    ps = ms.con.prepareStatement("SELECT c1,c2,c3,c4 FROM chart where id=?");
    ps.setString(1, no);
    rs = ps.executeQuery();
    if (rs.next()) {
        c1 = rs.getString(1);
        c2 = rs.getString(2);
        c3 = rs.getString(3);
        c4 = rs.getString(4);
    }
    System.out.println(c1 + c2 + c3 + c4);
    dataSet.setValue(Integer.parseInt(c1), "test", "Test");
    dataSet.setValue(Integer.parseInt(c2), "test", "Test");
    dataSet.setValue(Integer.parseInt(c3), "test", "Test");
    dataSet.setValue(Integer.parseInt(c4), "test", "Test");

    dataSet.setValue(Integer.parseInt(c1), "test1", "Test");
    dataSet.setValue(Integer.parseInt(c2), "test1", "Test");
    dataSet.setValue(Integer.parseInt(c3), "test1", "Test");
    dataSet.setValue(Integer.parseInt(c4), "test1", "Test");
   JFreeChart chart = ChartFactory.createBarChart(
            "Test", "Test", "Test",
            dataSet, PlotOrientation.VERTICAL, true, true, false);
    CategoryPlot plot = chart.getCategoryPlot();
    BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
    barRenderer.setSeriesPaint(0, Color.RED);
    return chart;

}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;

    Document document = new Document();

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                fileName));
        document.open();

        document.addCreator("Test");
        document.addSubject("Data Report");
        document.addCreationDate();
        document.addTitle("Please Read it");

        PdfContentByte contentByte = writer.getDirectContent();

        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height,
                new DefaultFontMapper());
        Rectangle2D rect = new Rectangle2D.Double(0, 0, width,
                height);
        chart.draw(graphics2d, rect);

        graphics2d.dispose();

        contentByte.addTemplate(template, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

}

RDY
  • 613
  • 1
  • 9
  • 25
  • 1
    What version of JCommon do you use? Looks like your version is too old. – Andrey E Dec 05 '14 at 07:47
  • A `NoSuchMethodError` often indicates that you use incompatible versions of your dependencies. – mkl Dec 05 '14 at 08:17
  • check your clashpath if you don't have same libraries with different version, then try to remove one and retry, then correct your configuration to keep only the good one when you deploy your app. – Mançaux Pierre-Alexandre Dec 05 '14 at 08:39
  • Thanks I change Jcommon and restart my server now its worked. – RDY Dec 05 '14 at 09:29
  • possible duplicate of [How do I fix a NoSuchMethodError?](http://stackoverflow.com/questions/35186/how-do-i-fix-a-nosuchmethoderror) – Erwin Bolwidt Dec 15 '14 at 13:36

0 Answers0