Home

JasperReport for Java web application

Creating Charts using database




This article teaches you how to embed JasperReports functionality in a JSP-based web application.

Getting ready

Download and install:

Set up your environment for Java programming languageDownload and set up Java on your machine. Java SE is freely available from the link Download  Java.You can download a version based on your operating system. After installing JDK, you will set the JAVA_HOME environment variable to point to the main folder of your JDK installation.

Eclipse - A Java IDE developed by the eclipse open-source community and can be downloaded from https://www.eclipse.org/


JasperStudio - To install jasper please refer previous article JasperReports Open Source Business Intelligence Solution


Database - I am using PostgreSQL 9.6 you can use any database you want. You just need to add jar file of the database you want to use and change the connection statments accordingly. 


Web ServerThe Apache Tomcat® software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. The Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket specifications are developed under the Java Community Process.

Setup and Install Apache Tomcat Server in Eclipse Development Environment (IDE)

Download Apache Tomcat from this link.

Binary Distributions core: zip (pgp, md5, sha1)

Open Eclipse Environment. Click on Servers Tab
Click on No servers are available. Click this link to create a new server...
Click Tomcat v9.0 Server and Next
It should be up and running on port 8080 and you could visit default page using URL: http://localhost:8080/
Download zip file and extract it.
Select Apache installation Directory and click Finish.
Now right click on Tomcat v9.0 Server at localhost [Stopped, Republish] under Servers and click Start.


Java web application


Now lets start the project.
Open Eclipse and click on the New tab and select web-> Dynamic web project
name it as vehiclesdata and finish.
Now download the folder and files present at this link-  https://github.com/rahulgupta-datascience/jasperreports.

You will be using jasperreports.jsp and vehicle.jrxml files in this application. You will find these files in the vehiclesdata/WebContent/.

Then Copy the complete vehiclesdata/WebContent/src folder and paste the 2 folders in java resources ->src folder of your eclipse project.

Next lets put up the required .jar files. Copy folder vehiclesdata/WebContent/WEB-INF/lib
and paste the lib folder in your project webcontent/web-inf folder. 

*Note i have provide lib folder with some extra .jar so as to about version conflict and also make it easy for you in future to used it in other projects.

Now we have only one thing left that is our data which is being used to create the chart in the report.

On the github link you must have seen two files one is database.txt that will help you to create the database. Second file is vehiclesdata.csv file which contain our data. This data will be inserted in the  created database.

Let see some important parts of the files we are using:

Jasperreports.jsp

String path2JRXMLFile = getServletContext().getRealPath("vehicle.jrxml");

This is line explain which jrxml is being used.
.jrxml is the design and format of your report which you want.
You can create your own .jrxml file click on your project and select NEW-> Jasper Report

Now a designer window will appear drag and drop the elements you want from the Palette column.

//Export PDF file to browser window
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport(); 

This above code make output export in Pdf form we can change according to your need like HTML etc.


ConnectionManager.java


    public static void main (String args[])
    {
        ConnectionManager db = new ConnectionManager("localhost", 5432, "postgres", "postgres", "root");
    }

This is where you mention connection details.

JasperReportsWrapper.java

public class JasperReportsWrapper
{
    private static File file = new File(".jrxml");
    public static String path2JRXMLFile = file.getAbsolutePath();
    public static String pdfFileName = "vehicle.pdf";
    public static String dbServerAdd = "localhost";
    public static int dbServerPort = 5432;
    public static String dbName = "postgres";
    public static String dbUser = "postgres";
    public static String dbPass = "root";
    private Connection connection = null;
    private ConnectionManager conManager = null;
    public JasperReportsWrapper ()   {
    }//JasperReportsWrapper

Here also you mention connection details.

This file is used to make the below code work present in .jsp file.


//Connect DB



JasperReportsWrapper wrapper = new JasperReportsWrapper();
wrapper.connect2DB(wrapper.dbServerAdd, wrapper.dbServerPort, wrapper.dbName, wrapper.dbUser, wrapper.dbPass);

 //Comiple JRXML file
 JasperReport jasperReport = wrapper.compileJRXMLFile(path2JRXMLFile);

 //Fill compiled JRXML file with data
 JasperPrint jasperPrint = wrapper.fillReport(jasperReport, null, wrapper.getConnection()); 

 //Set reponse content type
 response.setContentType("application/pdf");


Vehicle.jrxml

//Used to fetch data
<queryString>
<![CDATA[select State,year  from vehiclesdata]]>
</queryString>

//Insert the values into the pie-chart 
<field name="State" class="java.lang.String"/>
<field name="year" class="java.lang.Long"/>
<variable name="State" class="java.lang.String" resetType="None" calculation="Count">
<variableExpression><![CDATA[$F{State}]]></variableExpression>
</variable>
<variable name="year" class="java.lang.Integer" resetType="None" calculation="Sum">
<variableExpression><![CDATA[$F{year}]]></variableExpression>
</variable>

Output- Pdf file 





No comments:

Post a Comment