View Report Using iReport and Jasperreport in Java

0
54

Today’s Tutorial is all about “View Report Using iReport and Jasperreport in Java”.
This tutorial is all about “View Report Using iReport and Jasperreport in Java”. This tutorial will help you on how to display a report using iReport plugins in Java with Netbeans IDE within your program. I already discuss on how to install iReport plugins and create a report in Netbeans. Ensure that you already understand my last topics before you proceed this tutorial.

Please visit related topics

In Java programming, the common report plugins used is the iReport. The plugin can create a report file with jrxml extension and after compiling it, it generates another file with a jasper extension. The jrxml file will use in designing your report and adding data sources while the jasper file is use in viewing your report within the program you build.

View Report Using iReport and Jasperreport in Java Steps

Add or create a new form inside your source package. In my case, I named my form using “ViewReport” file name.

Design your new created form just look like the image below.

Download the following libraries from the internet using any browser and search engine and add it into your libraries list. The following libraries are commons.collections, commons.logging, commons-beanutils, commons-digester, groovy-all, and jasperreports.

Insert the following codes above your class to access the required libraries needed in viewing the report.

  1. import java.awt.Dimension;
  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.HashMap;
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9. import net.sf.jasperreports.engine.JRException;
  10. import net.sf.jasperreports.engine.JasperFillManager;
  11. import net.sf.jasperreports.engine.JasperPrint;

Insert the following codes below inside your class for variable declaration needed in accessing the MySQL database.

  1. Connection conn=null;
  2. ResultSet rs = null;

Double click the buttons inside your form and to generate the private method. Insert the following codes below to view the report.

  1. //Path to your .jasper file in your package
  2. String reportName = "Cruds/newReport.jasper";
  3. //Get a stream to read the file
  4. InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
  5. try {
  6. //Fill the report with parameter, connection and the stream reader
  7. JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
  8. //Viewer for JasperReport
  9. net.sf.jasperreports.swing.JRViewer jv = new net.sf.jasperreports.swing.JRViewer(jp);
  10. //Insert viewer to a JFrame to make it showable
  11. JFrame jf = new JFrame();
  12. jf.getContentPane().add(jv);
  13. jf.validate();
  14. jf.setVisible(true);
  15. jf.setSize(new Dimension(1024,768));
  16. jf.setLocation(1,1);
  17. jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
  18. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  19. } catch (JRException ex) {
  20. ex.printStackTrace();
  21. }

Run your program and the output should look like the image below.

When downloading the libraries from the internet, it is required to check the version of the iReport Designer you installed in Netbeans to avoid errors. For example, if using the iReport 4.7.1 plugins, you need to install also the jasperreports 4.7.1 library.

Remember that iReport and jasperReport work together to create and display your report. iReport is a designer and jasperRepor is for viewer. If you have questions and comments regarding on this topic, feel free to contact us.

Related Articles You May Like:

Facebook Comments
(Visited 59 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here