Java Reporting With Jasper Reports - Part 2
Architecture
As shown in the above figure JasperReports architecture is based on declarative XML files which by convention have an extension of jrxml that contains the report layout. A lot of third-party design tools were produced to generate your jrxml file in a smooth way (like iReport or JasperAssistant) Design file is supposed to be filled by report's result which is fetched from database, XML files, Java collection, Comma-separated values or Models. Jasper can communicate with those data-sources and more, it can merge any number of data-sources together and manipulates the results of any combinations. This communication goes through JDBC, JNDI, XQuery, EJBQL, Hibernate or existing Oracle PL/SQL. You also can define your own data-source class and pass it to jasper engine directly. After defining your report design layout in jrxml format and determining your data source(s) jasper engine does the rest of work. It compiles your design file and fills it with results fetched from data-source and generates your report to the chosen exporting format (PDF, Excel, HTML, XML, RTF, TXT …, etc.) Report Definition file structure (jrxml):
Jasper design file –jrxml- contains the following elements:- <jasperReport>: the root element.
- <title>: its contents are printed only once at the beginning of the report
- <pageHeader> - its contents are printed at the beginning of every page in the report.
- <detail> - contains the body of the report, repeated by n number of results
- <pageFooter> - its contents are printed at the bottom of every page in the report.
- <band> - defines a report section, all of the above elements contain a band element as its only child element.
Only the root element is mandatory, the rest of elements are optional.
Environment
To set up working environment we need to download JasperReport jar file from the following URL: http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579And add the following jars to your project classpath:
- jasperreports-2.0.4.jar
- commons-digester-1.7.jar
- commons-collections-2.1.jar (commons-collections.jar)
- commons-logging-1.0.2.jar
- commons-beanutils.jar
- iText-2.0.7.jar (used infor PDF exporting)
Sample application
At this section we'll introduce a sample application that generates PDF, HTML and Excel files contain the results of our report which is built over Oracle database contains the following table:| ITEM ITEM_ID ---- NUMBER(5) --- NOT NULL |
Result: Report should retrieve the items with amount less than or equal 100 item.
We're going to divide the work into two steps:
- Generate the report design (jrxml file).
- Implement application that assigns data source, compiles jrxml file and exports result in the chosen format.
Designing The Report
First we create new text file and rename it to sample_report.jrxml, this file should contain the following XML tags.
<!DOCTYPE jasperReport PUBLIC
"//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="sample_report" >
<queryString>
<![CDATA[select item_name,item_amount from item
where item_amount <=100]]>
</queryString>
<field name="ITEM_NAME" class="java.lang.String"/>
<field name="ITEM_AMOUNT" class="java.math.BigDecimal"/>
<columnHeader>
<band height="28" isSplitAllowed="true">
<staticText>
<reportElement x="40" y="11" width="193" height="15" key="staticText-1"/>
<text>
<![CDATA[Item Name]]>
</text>
</staticText>
<staticText>
<reportElement x="330" y="11" width="193" height="15" key="staticText-2"/>
<text>
<![CDATA[Item Amount]]>
</text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="27" isSplitAllowed="true">
<textField>
<reportElement x="47" y="6" width="173"
height="18" key="textField"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{ITEM_NAME}]]>
</textFieldExpression>
</textField>
<textField >
<reportElement x="330" y="6" width="100"
height="18" key="textField"/>
<textFieldExpression class="java.math.BigDecimal">
<![CDATA[$F{ITEM_AMOUNT}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The above XML file consists of the following main sections that defining report behavior and layout:
- <queryString>: contains the SQL statement which retrieves the report result.
- <field name>: defines the resulted fields from the query, and give them name to reuse them into the report body [they are case-sensitive].
- <staticText>: contains the header titles "Item Name" in <![CDATA[Item Name]]> tag format.
- <textFieldExpression>: defines the appearance of result field.
- $F{ITEM_NAME}: is a variable contains the value of Query result predefined field in the tag <field name>.
Once we finished the report design file, save it in C:\ directory.
Implementing The Report Service:
- Create a new java project.
- Import the jars listed in environment section to your project libraries.
- Create new class and import the following packages
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; 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.export.JRXlsExporter;
- Define the data source, in my case it's an oracle connection and established by JDBC as following:
public static Connection establishConnection()
{
Connection connection = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String oracleURL = "jdbc:oracle:thin:@localhost:1521:mySID";
connection = DriverManager.getConnection(oracleURL,"username","password");
connection.setAutoCommit(false);
}
catch(SQLException exception)
{
exception.printStackTrace();
}
return connection;
}Finally, the core code for compiling, filling and exporting the results in the following sequence:
- Define jasper objects that will hold report template, compiled files, and result files.
/* JasperReport is the object that holds our compiled jrxml file */ JasperReport jasperReport; /* JasperPrint is the object contains report after result filling process */ JasperPrint jasperPrint;
- Create a connection to my data-source; initialize the report parameter into empty HashMap then compile our jrxml file into JasperReport object and finally fill the JasperPrint object by data from data-source connection.
// connection is the data source we used to fetch the data from
Connection connection = establishConnection();
// jasperParameter is a Hashmap contains the parameters
// passed from application to the jrxml layout
HashMap jasperParameter = new HashMap();
// jrxml compiling process
jasperReport = JasperCompileManager.compileReport
("C://sample_report.jrxml");
// filling report with data from data source
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection); - Last segment of code is responsible for exporting the result files into different formats
// exporting process // 1- export to PDF JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report.pdf"); // 2- export to HTML JasperExportManager.exportReportToHtmlFile(jasperPrint, "C://sample_report.html" ); // 3- export to Excel sheet JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://simple_report.xls" ); exporter.exportReport();
You have just managed to generate your first jasper report in 3 different file formats at C:\\ directory (shown in the image below):
- sample_report.html
- sample_report.pdf
- sample_report.xls
Here we reach the end of today's article, next article we will cover the following points:
1- Using design tool (iReport) to generate robust and smooth jrxml file.
2- Create run-time search criteria and pass them to report.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
labidi chichi replied on Sun, 2008/08/24 - 5:27am
hello,
t the begining i was too happy by finding this tutorial, but after trying it , it doesn't work, there are 51 errors that talk about "symbols not found" , do you know the cause of these problems(i will send you the stack trace if you want), in fact i used commons-logging-1.1.jar instead of commons-logging-1.0.2.jar and itext-1.3.1.jar instead of iText-2.0.7.jar
i need your help,
thank you
Hossam Sadik replied on Sun, 2008/08/24 - 7:03am
@ chflb
I guess you didn't read the rest of articles, there were two more articles that cover working with designing tools to facilitate report design and remove the overhead of XML work.
you can find those articles at the following URL's:
http://java.dzone.com/articles/java-reporting-%E2%80%93-part-3
http://java.dzone.com/articles/java-reporting-%E2%80%93-part-4
try to figure them out, if you still facing problems don't hesitate to contact me directly.
labidi chichi replied on Sun, 2008/08/24 - 10:45am
in response to:
Hossam Sadik
hello hossam, thank you for your reply,
in fact,i have the jrxml file created, 'd like to say also that i use jsf+jpa
this is my code(the procedure responsable for compiling and exporting pdf) :
there are in result 51 errors,(perhaps they will be stupid errors) ;
labidi chichi replied on Sun, 2008/08/24 - 4:36pm
in response to:
labidi chichi
hello houssem,
i found the solution,in fact there is another jar to be imported, the jar is: jdt-compiler-X.jar,
thank you very much about these series,i hope next time you make a tutorial about how to use JasperServer, because i need now to put the generated pdfs in server to be consulted by the administrator,
any way, I thank you so much,
+@
gagan deep replied on Thu, 2009/02/05 - 7:54am
Hello Sir
I have created the jasper report using xml as shown in the above examplebut now i want to implement pagination in this report.how can i implement it.pls do reply .
thx
ali askari replied on Tue, 2009/05/12 - 1:55am
ali askari replied on Tue, 2009/05/12 - 1:58am
in response to:
labidi chichi
Hanen Mbarki replied on Tue, 2010/04/20 - 6:22am
Hello
I'm trying to develpper an application using netbeans and ireport
I use this tutorial to generate pdf file,but I have an
error : Language "null" is not supported by report compiler the .jrxml is correct and it generate a pdf if I use it onlyTamal Nayek replied on Sat, 2010/04/24 - 7:16am
Hi...
I use this code to generate Excel Report ....
//My written Code............
JasperReport jasperReport;
JasperPrint jasperPrint;
try{
Connection con= (Connection)dbcon.getdbConnection();
HashMap jasperParameter = new HashMap();
jasperReport = JasperCompileManager.compileReport("report/daily_stock_report.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, con);
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "E:/simple_report.xls" );
exporter.exportReport();
}
catch(Exception e)
{
e.printStackTrace();
}
*********************************************************************************
But I found some ERROR.....
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFCell.setEncoding(S)V
at net.sf.jasperreports.engine.export.JRXlsExporter.initCreateCell(JRXlsExporter.java:444)
at net.sf.jasperreports.engine.export.JRXlsExporter.createTextCell(JRXlsExporter.java:419)
at net.sf.jasperreports.engine.export.JRXlsExporter.exportText(JRXlsExporter.java:324)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportPage(JRXlsAbstractExporter.java:426)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReportToStream(JRXlsAbstractExporter.java:345)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReport(JRXlsAbstractExporter.java:197)
***************************************************************************************
Although I added all JAR Files......
1) iText-2.1.0.jar
2) jasperreports-3.5.1.jar
3) jfreechart-1.0.12.jar
4) commons-collections-2.1.1.jar
5) displaytag-1.2.jar
6) jakarta-oro.jar
7) jxl-2.6.jar
8) poi-3.2-FINAL.jar
9) commons-fileupload-1.0.jar
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
PLZ Help some one to execute the error less code...My Total Development is stopped with out generate
Excel Formate Report....... I am eagerly waiting forsome valuable reply....
*********************************************************************************
Ananda Jermin replied on Tue, 2010/07/27 - 4:08am
Java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
Can anyone please Help me with this error. It has made me not use my jasperreport in my program.
the code i use to generate the report is:
HashMap map = new HashMap();
Connection con = new connect().odbcConnect();
String reportFile = "E://Projects//NetBeansProjects//kusda//src//kusda//members.jrxml";
try {
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport report = JasperCompileManager.compileReport(jasperDesign);
JasperPrint print = JasperFillManager.fillReport(report, map, con);
JasperViewer.viewReport(print);
} catch (Exception ex) {
ex.printStackTrace();
}
from my tracing i get the source of the error is this line:
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
Can anyone debug this please? Thank u in advance.
Suresh Murthy replied on Tue, 2011/09/13 - 8:17am
Joguy Djou replied on Wed, 2011/10/12 - 2:48am
helo! Im trying to compile my my report on jrxml file source and getting this :
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:161)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForName(JRClassLoader.java:119)
at net.sf.jasperreports.engine.util.ClassUtils.instantiateClass(ClassUtils.java:57)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.instantiateRegistry(DefaultExtensionsRegistry.java:202)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:179)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.loadRegistries(DefaultExtensionsRegistry.java:139)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getRegistries(DefaultExtensionsRegistry.java:125)
at net.sf.jasperreports.extensions.DefaultExtensionsRegistry.getExtensions(DefaultExtensionsRegistry.java:102)
at net.sf.jasperreports.engine.component.ComponentsEnvironment.findComponentBundles(ComponentsEnvironment.java:94)
at net.sf.jasperreports.engine.component.ComponentsEnvironment.getCachedComponentBundles(ComponentsEnvironment.java:82)
at net.sf.jasperreports.engine.component.ComponentsEnvironment.getComponentBundles(ComponentsEnvironment.java:70)
at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.getSchemaLocations(JRReportSaxParserFactory.java:162)
at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.configureParser(JRReportSaxParserFactory.java:143)
at net.sf.jasperreports.engine.xml.JRReportSaxParserFactory.createParser(JRReportSaxParserFactory.java:108)
at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createParser(JRXmlDigesterFactory.java:1316)
at net.sf.jasperreports.engine.xml.JRXmlDigesterFactory.createDigester(JRXmlDigesterFactory.java:1291)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:203)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:152)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:150)
pleace sombody help me
Hitesh Kumar replied on Tue, 2012/02/21 - 8:56am
Thank you for your post Mr.Hossam Sadik,
it help me alot.. now I am able to generate my report in EXCEL format and also in CSV format.. without making any change in .jrxml code, before this I was generating PDF file
...
I have a simple code which create a pdf page in new window with .jrxml file
JasperReport jasperReport;
JasperPrint jasperPrint;
connection=MyConnection.getConnection(); /*Myconnection class here is used to establish connection that i have created. */
ServletContext sc1 = getServletContext();
Map jasperParameter = new HashMap();
ServletContext sc = getServletContext();
String rPath = sc.getRealPath("/WEB-INF/jsp/jasper/report23.jrxml");/* change path according to your project */
jasperReport = JasperCompileManager.compileReport(rPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);
byte [] abc=JasperExportManager.exportReportToPdf(jasperPrint);
ServletOutputStream ob=response.getOutputStream();
response.setContentType("application/pdf");
ob.write(abc,0,abc.length);
ob.flush();
ob.close();
Carla Brian replied on Fri, 2012/04/27 - 11:40pm
Raji Lakshmi replied on Mon, 2012/06/18 - 1:28am
in response to:
ali askari
I am using netbeans IDE and jasper report in linux OS. I added all needed .jar files and My code is,
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
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 com.mysql.jdbc.Statement;
public class sampleclass
{
public static void main(String g[])
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance ();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","ssgsoft");
System.out.println("Connection Established!");
InputStream input = new FileInputStream(new File("/home/ssg15/NetBeansProjects/sample/web/samplerep.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("ReportTitle", "PDF JasperReport");
System.out.println("Filling Report to File");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
OutputStream output = new FileOutputStream(new File("/home/ssg15/NetBeansProjects/sample/web/samplerep.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
System.out.println("Report Generation Complete");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
I got the output as
Connection Established!
18 Jun, 2012 11:54:12 AM org.apache.commons.digester.Digester error
net.sf.jasperreports.engine.JRException: org.xml.sax.SAXParseException: Document root element "jasperReport", must match DOCTYPE root "null".
SEVERE: Parse Error at line 2 column 405: Document root element "jasperReport", must match DOCTYPE root "null".
org.xml.sax.SAXParseException: Document root element "jasperReport", must match DOCTYPE root "null".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.rootElementSpecified(XMLDTDValidator.java:1621)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1900)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:764)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1363)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(XMLDocumentScannerImpl.java:1318)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.apache.commons.digester.Digester.parse(Digester.java:1647)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:239)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:226)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
at sampleclass.main(sampleclass.java:41)
BUILD SUCCESSFUL (total time: 0 seconds)
What is worng in this? Any one can explian or can solve my problem, Please..
Shumon Mandal replied on Fri, 2012/07/20 - 2:39am
I am using ireport for reporting tool as plugin in netbeans 7.1.1.
I have designed the jrxml file through ireport but unable to compile it to any format when using it in a jsp page.m getting a blank page output.
Here is my jrxml file code---------------
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="null" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="Title" forecolor="#FFFFFF" fontName="Arial" fontSize="26" isBold="true" pdfFontName="Helvetica-Bold"/>
<style name="SubTitle" forecolor="#666666" fontName="Arial" fontSize="18"/>
<style name="Column header" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
<style name="Detail" fontName="Arial" fontSize="12"/>
<queryString language="SQL">
<![CDATA[select * from tma_dept where dept ='Civil';]]>
</queryString>
<field name="sl_no" class="java.lang.Integer"/>
<field name="fname" class="java.lang.String"/>
<field name="mname" class="java.lang.String"/>
<field name="lname" class="java.lang.String"/>
<field name="age" class="java.lang.Integer"/>
<field name="dob" class="java.sql.Date"/>
<field name="doj" class="java.sql.Date"/>
<field name="dept" class="java.lang.String"/>
<field name="perm_addr" class="java.lang.String"/>
<field name="contact_num" class="java.lang.Long"/>
<field name="alt_contact" class="java.lang.Long"/>
<field name="phone_num" class="java.lang.Long"/>
<field name="pre_addr" class="java.lang.String"/>
<field name="city" class="java.lang.String"/>
<field name="state" class="java.lang.String"/>
<field name="pin_code" class="java.lang.String"/>
<field name="gender" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="70" splitType="Stretch">
<image>
<reportElement x="-21" y="5" width="595" height="64"/>
<imageExpression class="java.lang.String"><![CDATA["wood.jpg"]]></imageExpression>
</image>
<staticText>
<reportElement style="Title" x="0" y="5" width="263" height="33"/>
<textElement verticalAlignment="Middle"/>
<text><![CDATA[Mahavir AutoVation]]></text>
</staticText>
<staticText>
<reportElement style="SubTitle" x="128" y="42" width="200" height="22" forecolor="#FFFFFF"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Department Report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="36" splitType="Stretch">
<line>
<reportElement positionType="FixRelativeToBottom" x="0" y="35" width="555" height="1"/>
<graphicElement>
<pen lineWidth="0.5" lineColor="#999999"/>
</graphicElement>
</line>
<staticText>
<reportElement style="Column header" x="0" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[sl_no]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="32" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[fname]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="64" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[mname]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="96" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[lname]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="128" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[age]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="160" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[dob]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="192" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[doj]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="224" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[dept]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="256" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[perm_addr]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="288" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[contact_num]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="320" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[alt_contact]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="352" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[phone_num]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="384" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[pre_addr]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="416" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[city]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="448" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[state]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="480" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[pin_code]]></text>
</staticText>
<staticText>
<reportElement style="Column header" x="512" y="19" width="32" height="15"/>
<textElement/>
<text><![CDATA[gender]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="16" splitType="Stretch">
<line>
<reportElement positionType="FixRelativeToBottom" x="0" y="15" width="555" height="1"/>
<graphicElement>
<pen lineWidth="0.5" lineColor="#999999"/>
</graphicElement>
</line>
<textField>
<reportElement style="Detail" x="0" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{sl_no}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="32" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{fname}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="64" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{mname}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="96" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{lname}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="128" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="160" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{dob}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="192" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{doj}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="224" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{dept}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="256" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{perm_addr}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="288" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{contact_num}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="320" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{alt_contact}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="352" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{phone_num}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="384" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{pre_addr}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="416" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{city}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="448" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{state}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="480" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{pin_code}]]></textFieldExpression>
</textField>
<textField>
<reportElement style="Detail" x="512" y="0" width="32" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{gender}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch">
<line>
<reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
<graphicElement>
<pen lineWidth="0.5" lineColor="#999999"/>
</graphicElement>
</line>
</band>
</columnFooter>
<pageFooter>
<band height="20" splitType="Stretch">
<textField>
<reportElement style="Column header" x="433" y="0" width="80" height="20"/>
<textElement textAlignment="Right">
<font size="10" isBold="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement style="Column header" x="513" y="0" width="40" height="20"/>
<textElement>
<font size="10" isBold="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<textField pattern="EEEEE dd MMMMM yyyy">
<reportElement style="Column header" x="0" y="0" width="197" height="20"/>
<textElement>
<font size="10" isBold="false"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
Here is my jsp code------------------
<%@page import="net.sf.jasperreports.view.JasperViewer"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<body >
<%@page import="java.lang.*"%>
<%@page import= "java.sql.Connection"%>
<%@page import= "java.sql.DriverManager"%>
<%@page import= "java.sql.*"%>
<%@page import= "java.util.HashMap"%>
<%@page import= "net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import= "net.sf.jasperreports.engine.JasperCompileManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperPrint"%>
<%@page import= "net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperReport" %>
<%@page import= "net.sf.jasperreports.engine.export.*"%>
<%@page import= "net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import= "net.sf.jasperreports.engine.JREmptyDataSource"%>
<%@page import= "net.sf.jasperreports.engine.JRException"%>
<%
String input = "./report/dept_report.jrxml";
String output = "./report/dept_report.html";
Connection con = null;
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tma","root","root");
out.println("inside con");
HashMap jasperParameter = new HashMap();
out.println("inside hash map");
jasperReport = JasperCompileManager.compileReport(input);
out.println("inside jasperReport"+jasperReport);
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, con);
JasperExportManager.exportReportToHtmlFile(jasperPrint,output);
//JasperViewer.viewReport(jasperPrint);
//JRXlsExporter exporter = new JRXlsExporter();
//exporter.exportReport();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
con.close();
}
%>
</body>
</html>
KINDLY SUGGEST ME WHERE AM I WRONG?
Shumon Mandal replied on Fri, 2012/07/20 - 2:48am
Swapnil Shirsat replied on Sat, 2012/08/25 - 8:42am
I am getting this error when i am trying to run the above code
please help me out.
I am using eclipse IDE and ireport 4.7.0 tool
Exception in thread "main" net.sf.jasperreports.engine.JRException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:247)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:226)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:152)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:151)
at demo.main(demo.java:45)
Caused by: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.commons.digester.Digester.parse(Digester.java:1647)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:239)
... 6 more
Steve Kalenga replied on Thu, 2012/09/06 - 2:51am
hi i did use this tutorial
i using jdevloper 11 release 2 adf and oracle 11g
when i run the report it return just an empty pdf,html and excel
the code have the jasperparameter underline with message "unchecked conversion from hashmap to <string,object> unsound but tolorated"
could anyone check my code and tell me where did i miss
package deployPro.view;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.SQLException;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
import java.sql.DriverManager;
@ManagedBean(name="CollegeReportsBean")
@RequestScoped
public class reportsClass {
/* JasperReport is the object
that holds our compiled jrxml file */
JasperReport jasperReport;
/* JasperPrint is the object contains
report after result filling process */
JasperPrint jasperPrint;
public reportsClass() {
}
public static Connection establishConnection() throws ClassNotFoundException {
Connection connection = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String oracleURL = "jdbc:oracle:thin:@localhost:1521:xe";
connection = DriverManager.getConnection(oracleURL,"username","password");
connection.setAutoCommit(false);
}
catch(SQLException exception)
{
exception.printStackTrace();
}
return connection;
}
public void welcome(ActionEvent actionEvent) throws FileNotFoundException, JRException, NamingException, SQLException, IOException, ClassNotFoundException {
// connection is the data source we used to fetch the data from
Connection connection = establishConnection();
// jasperParameter is a Hashmap contains the parameters
// passed from application to the jrxml layout
HashMap jasperParameter;
jasperParameter = new HashMap();
// jrxml compiling process
jasperReport = JasperCompileManager.compileReport("C:\\JDeveloper\\mywork\\CollegeInformationSystem\\CollegeViewController\\report2.jrxml");
// filling report with data from data source
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);
// exporting process
// 1- export to PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report1.pdf");
// 2- export to HTML
JasperExportManager.exportReportToHtmlFile(jasperPrint, "C://sample_report.html" );
// 3- export to Excel sheet
JRXlsExporter exporter;
exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://simple_report.xls" );
exporter.exportReport();
}
}
Virendr Patidar replied on Tue, 2012/10/02 - 12:07am
Plzzzz
any one can send complete code for jasper report and how i compile it
Virendra341@gmail.com
Farooq Shaikh replied on Fri, 2012/10/05 - 12:53am
HI ALL...
Shumon Mandal: if u got the solution let me kno .. am using jsp to call jrxml ..
farooqsha27@gmail.com
Here is my jsp code------------------
<%@page import="net.sf.jasperreports.view.JasperViewer"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<body >
<%@page import="java.lang.*"%>
<%@page import= "java.sql.Connection"%>
<%@page import= "java.sql.DriverManager"%>
<%@page import= "java.sql.*"%>
<%@page import= "java.util.HashMap"%>
<%@page import= "net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import= "net.sf.jasperreports.engine.JasperCompileManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperPrint"%>
<%@page import= "net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import= "net.sf.jasperreports.engine.JasperReport" %>
<%@page import= "net.sf.jasperreports.engine.export.*"%>
<%@page import= "net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import= "net.sf.jasperreports.engine.JREmptyDataSource"%>
<%@page import= "net.sf.jasperreports.engine.JRException"%>
<%
String input = "./report/dept_report.jrxml";
String output = "./report/dept_report.html";
Connection con = null;
JasperReport jasperReport;
JasperPrint jasperPrint;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tma","root","root");
out.println("inside con");
HashMap jasperParameter = new HashMap();
out.println("inside hash map");
jasperReport = JasperCompileManager.compileReport(input);
out.println("inside jasperReport"+jasperReport);
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, con);
JasperExportManager.exportReportToHtmlFile(jasperPrint,output);
//JasperViewer.viewReport(jasperPrint);
//JRXlsExporter exporter = new JRXlsExporter();
//exporter.exportReport();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
con.close();
}
%>
</body>
</html>
Farooq Shaikh replied on Fri, 2012/10/05 - 12:55am
in response to:
Shumon Mandal