How to show report with JasperReports API

2019-09-06 00:11发布

问题:

I am using NetBeans 7 and developing java database application. For the reporting purpose I have installed the iReport 4 plug-in in NetBeans 7 and designed the very simple report of my application. How can I call the report through a button call.

Few days later of this question what i have done is:

I downloaded the JAR file and added it to the Netbeans after that i did the following coding in a button but as i click the button it gives this error:

'Exception in thread "AWT-EventQueue-0"     java.lang.Error:'java.lang.reflect.InvocationTargetException'

And this is is the code which i have done on a button event click:

try {
    String reportName = "prisonersdbv1/reports/newReport.jasper";
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/APD", "root", "khan");
    JasperPrint jasperPrint = JasperFillManager.fillReport(is, new HashMap(), conn);
    JasperViewer jv = new JasperViewer(jasperPrint);
    jv.setVisible(true);
} catch (JRException ex) {
    Logger.getLogger(PrisonersDBv1View.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
    Logger.getLogger(PrisonersDBv1View.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
    Logger.getLogger(PrisonersDBv1View.class.getName()).log(Level.SEVERE, null, ex)
}

回答1:

For viewing report with help of JasperReports API you can use the net.sf.jasperreports.view.JasperViewer class.

The sample of using:

JasperViewer.viewReport("sampleReport.jrprint", false, true, locale); 

You can find the full sample of using JasperViewer in $jasperreports$\demo\samples\i18n folder from the JasperReports distribution package.