View Report Using iReport and Jasperreport in Java

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.

[java]import java.awt.Dimension;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JTextField;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;[/java]

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

[java]Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;[/java]

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

[java]//Path to your .jasper file in your package
String reportName = “Cruds/newReport.jasper”;
//Get a stream to read the file
InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
try {
//Fill the report with parameter, connection and the stream reader
JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
//Viewer for JasperReport
net.sf.jasperreports.swing.JRViewer jv = new net.sf.jasperreports.swing.JRViewer(jp);
//Insert viewer to a JFrame to make it showable
JFrame jf = new JFrame();
jf.getContentPane().add(jv);
jf.validate();
jf.setVisible(true);
jf.setSize(new Dimension(1024,768));
jf.setLocation(1,1);
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
} catch (JRException ex) {
ex.printStackTrace();
}[/java]

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.

About The View Report Using iReport and Jasperreport In Java

Project Name:View Report Using iReport and Jasperreport
Language/s Used:JAVA
Database:None
Type:Desktop Application
Developer:IT SOURCECODE
Updates:0
View Report Using iReport and Jasperreport In Java– Project Information

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:

Leave a Comment