Creating Report Using JasperReports
page 4 of 9
by Anuva Das
Feedback
Average Rating: 
Views (Total / Last 10 Days): 39961/ 654

Designing Report

JasperReports uses XML file for creating report. The user designs a report using respective XML tags and attributes, defined in a file called jasperreports.dtd. Save the file with a JRXML/XML extension. This file contains all the information about the report such as title, page headers and footers, column headers and footers, summary, query, fonts, images, report details, etc.

For example, the following XML template is used to design a report that will show the salesman details with a report title that has been given as a parameter.

Listing 1

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
 "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="demo">
<parameter name="Report_Title" class="java.lang.String"/> 
<queryString><![CDATA[select * from sales_master]]></queryString>
<field name="SALESMANNO" class="java.lang.String"/>
<field name="SALESMAN_NAME" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<field name="STATE" class="java.lang.String"/>
<field name="QTY_SALES" class="java.math.BigDecimal"/>
<title>     
<band height="50" >
<textField >
<reportElement x="230" y="4" width="100" height="18"/> 
<textFieldExpression class="java.lang.String">
<![CDATA[$P{Report_Title}]]>
</textFieldExpression>  
</textField>
</band>
</title>
<columnHeader>
<band height="30">
<staticText>
<reportElement x="29" y="0" width="102" height="25"/> 
<text><![CDATA[Salesman Name]]></text>
</staticText>
<staticText>
<reportElement x="146" y="2" width="104" height="23"/>
<text><![CDATA[City]]></text>
</staticText>
<staticText>
<reportElement x="260" y="1" width="100" height="24"/> 
<text><![CDATA[State]]></text>
</staticText>
<staticText>
<reportElement x="383" y="1" width="94" height="23"/> 
<text><![CDATA[Quantity Sold]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="100">
<textField>
<reportElement x="28" y="8" width="100" height="29"/> 
<textFieldExpression class="java.lang.String">
<![CDATA[$F{SALESMAN_NAME}]]>
</textFieldExpression>
</textField>
<textField >
<reportElement x="146" y="11" width="100" height="25" /> 
<textFieldExpression class="java.lang.String">
<![CDATA[$F{CITY}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="263" y="11" width="100" height="24" />
<textFieldExpression class="java.lang.String">
<![CDATA[$F{STATE}]]>
</textFieldExpression>
</textField>
<textField >
<reportElement x="384" y="12" width="100" height="24" /> 
<textFieldExpression class="java.math.BigDecimal">
<![CDATA[$F{QTY_SALES}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

Brief descriptions of the most important sections for designing a report are given below.

title

As the name implies, this section contains the report title and appears at the top of the report.

queryString

This section defines the SQL query to obtain data for report filling process. This section of the XML template does not contain a height or a width.

pageHeader

This section appears at the top of each page in the report and can be used to display information such as dates, page numbers, etc.

columnHeader

This section is used to display the information as heading for each column.

detail

This is the body of the report. It contains data from the data repository.

columnFooter

This section goes at the bottom of each column and can be used to display information such as the total number of records.

pageFooter

This section appears at the bottom of each page in the report.

Summary

This section contains the information that will be displayed at the end of the report.

Now, let us see how to use JasperReports in Java applications. To create JasperReports report, this JRXML/XML file needs to be loaded. A JasperDesign Object represents the report design. The following codes illustrate this.

Listing 2

JasperDesign jasperDesign = JRXmlLoader.load (
C:/jasperReports/demo.jrxml);

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 1 and type the answer here:

User Comments

Title: QUERY STRING   
Name: nathan
Date: 1/1/2009 11:29:36 PM
Comment:
hai can't two query string using one jrxml file if do you need subreport using can be used.
Further Details : kolanchinathan.e@gmail.com

thanks & regarding
kolanchinathan.R
Title: retrieve the variable into the report   
Name: sathya
Date: 12/27/2008 6:25:34 AM
Comment:
i need a help to add the variable value into the ireport?
any one help me
reply me to subarajan_85@yahoo.co.in
Title: add chart into report   
Name: mummoorthi
Date: 12/24/2008 1:09:11 AM
Comment:
i new to ireport. i want to take report for such record for ms access database with chart. anyone can guide me please
Title: xplain more detail on point 4   
Name: anaRodhi
Date: 11/3/2008 8:32:49 PM
Comment:
plz xplain more details on point 4..
Title: how to run a Jasper report   
Name: eddiegeerts@hotmail.com
Date: 11/1/2008 9:53:06 AM
Comment:
Hi, I would like to run a jasper report (.jrxml/.jasper file) by asp.net/vb.net, eg. I want to run the jasperreport and the output (html/pdf) must be stored in a directory.
Can anyone me provide some examples of how to do this ??
Title: image symbols in jasper html report   
Name: v.naresh8@gmail.com
Date: 10/11/2008 6:58:54 AM
Comment:
i developed a html report using jasper successfully..but, in that html source code automatically
"img alt="" src="nullpx"

please send the solution to my mail
Title: Using two query strings in jrxml   
Name: Lavanya
Date: 9/24/2008 1:50:38 AM
Comment:
Can we use two query strings in jrxml?
Title: i can't make a jasper report can any body help me????   
Name: chokz_1009@yahoo.com
Date: 9/7/2008 11:46:10 PM
Comment:
can any one help me how to make a jasper report using java....

thx... f ever xend on my account plz.... chokz_1009@yahoo.com
Title: annot make pages of report   
Name: Paresh
Date: 9/2/2008 12:41:46 AM
Comment:
I am making an inventory software. I pick up data for reports from database. Can anyone please tell me how to present it in pages and also how to add code to print. Please reply at parfive_05@yahoo.co.in
Title: Cannot Load .jrxml from my project   
Name: kathu
Date: 6/12/2008 3:51:16 AM
Comment:
When i JasperReport jasperReport = JasperCompileManager
.compileReport(jasperDesign);& load my .jrxml in my application it throws io.file not found exception plz help
Title: Jasper with ASP.NET   
Name: Prince
Date: 5/20/2008 8:44:13 AM
Comment:
Hi..
I have created a DEMO.jrxml file using ireports.. now i need to integrate it with my asp.net application.. could anyone suggest a gud url for doing this?

how can i deploy it? and how do i pass the parameter values as querystring inputs for the same?
Title: Jasper with Asp.net   
Name: Jack
Date: 3/18/2008 4:24:25 AM
Comment:
How can I use JasperReports along with Asp.net?
Title: two query in one jrxml file   
Name: Hum
Date: 3/17/2008 8:59:49 AM
Comment:
Is it possible to have two multiple queries in one jrxml file? THanks.
Title: Generating Report in PDF or XLS format in Swing application(JAVA)   
Name: uday
Date: 3/3/2008 5:05:56 AM
Comment:
hi,

i am retrieving the data(reports) to embedded database but i need to how to database selected table data export to xls format. plz help me swings code. i'm using netbeans ide.please reply me on udaymyadam@gmail.com
Title: Reportunit   
Name: Aadam
Date: 2/19/2008 4:34:43 AM
Comment:
HI
i have created the reports and tried to connect the report using the reportunit concept,when i executed the report the second report which is linked through the frist report show all the data without taking the the condition.

ex:i have clicked on the perticular field say emp name(xyz) the second report should show the result of that [xyz] employee.
Could any one help on this

regds
Aadam
Title: jasper Rpeort   
Name: Nilesh
Date: 1/25/2008 6:59:31 AM
Comment:
Hi can u any one tell me how to create dynamic header in jasper Report and can we execute two query in one jrxml file
Title: Error in executing jasper report   
Name: Gandhi
Date: 11/27/2007 12:24:09 AM
Comment:
hi,
i have created japser report and it works fine in local server. but i encounter problem while executing in server linux .. please help me out to solve this . i have included all the necessary jar files too... i am getting the following error


net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
public class EnquiryListReport_1196137097748_696339 extends JREvaluator
<--------->
1 errors

at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:193)
at net.sf.jasperreports.engine.design.JRDefaultCompiler.compileReport(JRDefaultCompiler.java:105)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:212)
at com.amslogic.reports.EnquiryList.doGet(EnquiryList.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:272)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:161)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:245)
at org.apache.catalina.core.ApplicationFilterChain.access$0(ApplicationFilterChain.java:177)
at org.apa
Title: Generating Report in PDF or XLS format in Dot NET enviroment   
Name: Zaraq
Date: 11/16/2007 5:57:59 AM
Comment:
could u please help me by updating me the info. i m working on Anti Norcotics force Database where they do keep the records of suspects n criminal. i wanted to know that how could we genenrate reports in PDF or XLS format. please reply me on zarak18@hotmail.com thanks
Title: Jasper in Swing reply   
Name: kruger
Date: 10/20/2007 5:54:28 AM
Comment:
yes jasper can be used in swing application
Title: Jasper in Swing   
Name: yanti
Date: 10/4/2007 2:54:48 AM
Comment:
Hai,,hai,,,Friends please help me,
can Jasper in use in Swing Application??
thanks be4..
Title: jasperreports in struts2   
Name: pratap
Date: 9/26/2007 12:46:01 PM
Comment:
how to generate jasperreports using struts2 in order to retrieve data from database
Title: WEBFOCUS   
Name: Valerie Young
Date: 9/6/2007 9:22:34 AM
Comment:
In the webfocus training we touched basics on standard reports, crating reports, sharing reports and olap reports.
Title: Well Article   
Name: Rajesh Das
Date: 8/24/2007 4:41:08 AM
Comment:
I need Sample code for this report using through Applet.
Title: One more Query..   
Name: Kranthi Kiran
Date: 8/23/2007 12:07:48 PM
Comment:
Anuva,

where are we specifying connection attributes in jrxml file.. is it not required? How does it compile?

Regards,
Kranthi
Title: Well articulated article..   
Name: Kranthi Kiran
Date: 8/23/2007 11:46:27 AM
Comment:
Hi Anuva,

Thanks for the wonderful, simple article. My questions are--
1#Is JDBC conn. mandatory..? we are using Hibernate and through DAO I get List of objects,I want to iterate the list and print. How can I do this?

2#Do we have to compile jrxml every time we run?

3# I just tried with Jasper Assistant and I genereted pdf and how can I use that jasper file in my servlet?

Regards,
Kranthi
Title: Error   
Name: Prabha
Date: 7/11/2007 12:40:36 AM
Comment:
Iam trying to copy that xml file nad pasting here but iam geeting the following error

We apologize for the inconvenience. This error has been logged and our support staff has been notified.
Thanks, AspAlliance Staff
Title: reply to Prabha   
Name: anuva
Date: 7/10/2007 1:33:27 AM
Comment:
Send me ur XML data source.So that i can test it.
Title: Error   
Name: Prabha
Date: 7/10/2007 12:14:15 AM
Comment:
I used the database connection and tried your code.I have a table with two columns 'name' and 'age'.But ian getting the same error.
java.lang.NoSuchMethodError: org.jfree.chart.ChartFactory.createPieChart(Ljava/lang/String;Lorg/jfree/data/general/PieDataset;ZZZ)Lorg/jfree/chart/JFreeChart;
at net.sf.jasperreports.engine.fill.JRFillChart.evaluatePieImage(JRFillChart.java:1525)
at net.sf.jasperreports.engine.fill.JRFillChart.evaluateImage(JRFillChart.java:712)
at net.sf.jasperreports.engine.fill.JRFillChart.evaluate(JRFillChart.java:669)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:277)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:399)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummaryNewPage(JRVerticalFiller.java:1031)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummary(JRVerticalFiller.java:772)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportEnd(JRVerticalFiller.java:259)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:135)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:758)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:666)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:63)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:402)
at Servlet_graph.main(Servlet_graph.java:28)
Exception in thread "main"
Title: Error   
Name: Prabha
Date: 7/8/2007 10:56:42 PM
Comment:
Iam trying to send the xml file but some error is displaying.I couldnt send it.
Title: To Prabha   
Name: Anuva
Date: 7/6/2007 1:33:16 AM
Comment:
Could u plz send me ur XML data source?? So that i can test it.
Title: Error   
Name: Prabha
Date: 7/6/2007 1:10:15 AM
Comment:
No. Actaully i have included jFreechart.Other programs related to jfreechart works fine.
Title: reply to prabh   
Name: Anuva
Date: 7/5/2007 7:49:07 AM
Comment:
i think u have not included the JfreeChart jar file on ur classpath.First set the classpath for it.
Title: Error   
Name: Prabha
Date: 7/5/2007 3:36:06 AM
Comment:
Thanks again.i tried your code.But iam getting the following error:
java.lang.NoSuchMethodError: org.jfree.chart.ChartFactory.createPieChart(Ljava/lang/String;Lorg/jfree/data/general/PieDataset;ZZZ)Lorg/jfree/chart/JFreeChart;
at net.sf.jasperreports.engine.fill.JRFillChart.evaluatePieImage(JRFillChart.java:1525)
at net.sf.jasperreports.engine.fill.JRFillChart.evaluateImage(JRFillChart.java:712)
at net.sf.jasperreports.engine.fill.JRFillChart.evaluate(JRFillChart.java:669)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:277)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:399)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummarySamePage(JRVerticalFiller.java:834)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummary(JRVerticalFiller.java:768)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportEnd(JRVerticalFiller.java:259)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:135)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:758)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:685)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
at chart4.main(chart4.java:40)
Exception in thread "main"
Title: Reply to Prabha   
Name: Anuva
Date: 7/5/2007 2:07:03 AM
Comment:
This is the java code for creating chart.

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.view.JasperDesignViewer;

public class chart{
public static void main(String[] args){
Connection jdbcConnection = null;
JasperReport jasperReport;
JasperPrint jasperPrint;
JasperDesign jasperDesign;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
jdbcConnection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.85:1521:oracledb",
"xxx","yyy");
jasperDesign = JRXmlLoader.load ("C:/ireportforarticle/pieChart.jrxml");
jasperReport = JasperCompileManager.compileReport(jasperDesign);
jasperPrint = JasperFillManager.fillReport(jasperReport,null,jdbcConnection);
JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/iReportExample/chart/pieChart.pdf");
//for creating report in html format
JasperExportManager.exportReportToHtmlFile(jasperPrint,"C:/iReportExample/chart/piechart.html");
//To view report using JasperViewer
JasperViewer.viewReport(jasperPrint);
}catch (SQLException ex){ex.printStackTrace();
}catch (ClassNotFoundException ex){ex.printStackTrace();
}catch (JRException e){e.printStackTrace();
}}}

U have to include JCommon jar and JFreeChart jar files on ur class path.
Hope this will help u.
Title: reports for graph(thanks anuva)   
Name: prabha
Date: 7/3/2007 4:52:28 AM
Comment:
thanks for your reply.But iam not using servlets in my code.I have to write a java code and using xml datasource.Do you have samples for it?Should i have to import JFreechart for creating charts?Kindly help.
Title: reply to Prabha   
Name: Anuva
Date: 6/29/2007 6:36:23 AM
Comment:
First design a JRXML file that contain ur Chart, then use the following servlet code to create a report in pdf/html.

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.export.*;

public class chart extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
ServletOutputStream servletOutputStream = response.getOutputStream();
Connection conn = null;
JasperReport jasperReport;
JasperPrint jasperPrint;
JasperDesign jasperDesign;
try{
// get a database connection
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection
("jdbc:oracle:thin:@192.168.10.85:1521:oracledb",
"xxx","yyy");
// load JasperDesign and compile it into JasperReport
jasperDesign = JRXmlLoader.load("C:/pieChart.jrxml");
jasperReport = JasperCompileManager.compileReport(jasperDesign);
// fill JasperPrint using fillReport() method
jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);
JasperExportManager.exportReportToPdfFile(jasperPrint,"C:/piechart.pdf");
response.setContentType("application/pdf");
//for creating report in html format
JasperExportManager.exportReportToHtmlFile(jasperPrint,"C:/piechart.html");
JasperExportManager.exportReportToPdfStream
(jasperPrint, servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();
}
catch(SQLException sqle){
System.err.println(sqle.getMessage());}
catch (ClassNotFoundException e){
Title: reports for greaph   
Name: Prabha
Date: 6/29/2007 5:25:31 AM
Comment:
Hi,

It would be useful for me if you provide tha java code for filling the data to a chart/graph and then producing the final report in pdf/html etc.Also mention the classes and packages to be included.
Title: good Work   
Name: Ramesh
Date: 6/26/2007 5:07:41 PM
Comment:
Hi,

This is really a nice piece of work
Title: report   
Name: boikanyo
Date: 5/15/2007 4:22:14 AM
Comment:
i'm very thank full for this article.it made my work easier.
its a good 1 it deserves a wow.

but hey how about a 1 that shows how to disply that charts on a browser using pdf.

that will be everything.
Title: Fine   
Name: jignesh
Date: 4/4/2007 2:52:25 AM
Comment:
Thanks very useful demostration.
Title: Creating Report Using JasperReports   
Name: jignesh
Date: 4/4/2007 2:52:20 AM
Comment:
Thanks very useful demostration.
Title: Overally a great article.   
Name: debayan
Date: 3/19/2007 1:24:36 AM
Comment:
The thing that mosts interests me is Jasper's ability to export reports to such a large no. of file formats which makes it trully an ideal choice for people working in Java platform. However a brief discussion about JasperReport's operating-environment requirement would have made the
article absolutely complete. Anyway great stuff and very detailed coverage on the process of report generation makes the article very useful for J2EE people.
Title: Good job   
Name: Sandeep
Date: 3/15/2007 11:52:45 PM
Comment:
Good job. Keep moving ahead...
Title: Excellent   
Name: Chandrakanta Kar
Date: 3/15/2007 12:26:57 PM
Comment:
It is very useful for the developers work in J2EE platfrom.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/6/2009 11:11:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search