I’ve made a previous article about setting up your environment to be a “JasperReport-ready”. You can click here to recall it.
And now, in this article, i will guide you to build your first JasperReport application. For this part, i will split this tutorial into 2 parts.
1. Let’s setup a dummy database. I’m using MySQL 5.0. Suppose we have a database named db_jasper
CREATE TABLE employees (
employee_id VARCHAR(6) NOT NULL PRIMARY KEY,
employee_name VARCHAR(30),
salary INT(6)
);
INSERT INTO employees VALUES ('E0001','Hans Kristanto',5000000);
INSERT INTO employees VALUES ('E0002','Conrad Alvin',3500000);
INSERT INTO employees VALUES ('E0003','Luna Hu',4500000);
INSERT INTO employees VALUES ('E0004','Jina Seo',3650000);
INSERT INTO employees VALUES ('E0005','Ivan Lu',4510000);
INSERT INTO employees VALUES ('E0006','Dennis Ewing',500000);
2. Start your iReport designer.
3. Create a new report template
Click on File -> New, then choose any template paper. For example, Blank A4 (in iReport 3.7.0). For iReport 3.7.0 below, just follow the wizard as usual.
NOTES : For a while, never use Report Wizard! You have to do it manually.
Give it name and directory to be saved. Click on finish.
4. After that, you will see window like this
If you to the report designer, there are 7 sections. I will explain it one by one :
- Title : is section for placing title document. Title document will only appear on the first page of document.
- Page Header : is section for placing page header, for example, page number, title, etc. This header will appear in every page of report but not in the first page.
- Column Header : is section for placing column header for detail title, for example, Employee Name, Salary. You will see the different in the real application.
- Details : is section where you place all data. Detail section will iterate as much as fetched data. Again, you will see how it works in the real application.
- Column Footer : similar with Column Header, but placed in the foot.
- Page Footer : similar with Page Header, but placed in the foot.
- Summary : is section where you put all summarize. For example, summary of total employees, total profits/loss, etc.
You have to understand these 7 sections first before we continue to the next step. Already understand? let’s go ahead!
5. Create a datasource
in the toolbar, you will see an database icon with a plug in front of it.
. You will see a new window appear :
Click on New button on the right side, then follow the wizard. My suggestion :
- use Database JDBC Connection.
- give it name afterward.
- use com.mysql.jdbc.Driver as the JDBC Driver.
- type your database name on JDBC URL e.g. : jdbc:mysql://localhost/db_jasper
- type your username and password.
After that, you will see the drop down next to the database icon will be set into our new datasource :
Everything is done for setting up. Let’s continue for the next core tutorial on the part 2.






diablo13
February 27, 2010 at 11:09 pm
Still waiting for part 2 impressive application
Oya, in the next chapter (part) please give an example and simple code in Java. So that my desktop application for example, can use and call these jasperReport and print it appropriate to designing report that has been build in iReport.
regards.
Game
April 2, 2010 at 9:33 pm
Thanks for the informative post, found it really useful. ref And now, in this article, i will guide you to build your first JasperReport application. For this part, i will split this tutorial into 2 parts.
Preludz
June 3, 2010 at 4:35 pm
Hi,
Nice tutorial but I’ve a question.
You wrote : “Page Header : is section for placing page header, for example, page number, title, etc. This header will appear in every page of report but not in the first page.”
In fact, the page header do appear on the forst page, do you know why and how it is possible to not display it on the forst page ?
Thanks
Hans Kristanto
June 3, 2010 at 8:07 pm
Hi Preludz,
i think i’ve made a mistake in writing detail about page header section since it will always shown in every report page. i’ve made some investigation about this and i still didn’t find the way to disable the first page header. i think it’s quite make sense enough since page header is an information needed to be printed in every page such as the day you print the report and so on. I really apologize about this mistake and i will repair it soon.
Regards
Preludz
June 3, 2010 at 8:29 pm
Hans,
I found a way to resolve my problem by editing the report properties with the option : “Title on a new page”.
Thanks a lot,
Regards
veja
January 24, 2011 at 1:57 pm
Hi
I have a column header that is showing on page two even when data is only showing for page one. Can you please help to control a column header to only show only if there’s data ,on a second page. Thanks
Regards
Hans Kristanto
January 24, 2011 at 4:33 pm
Based on my experience, the things that you need to modify is in the layout, which means if you have only 1 page data but it showed 2 pages, means that there are whitespaces in your layout. i don’t know wether we can control page directly from code.
sunjavastar
October 18, 2011 at 2:38 pm
can u explain how to use sub reports in jasper
Hans Kristanto
October 18, 2011 at 5:16 pm
Theoritically using sub report in Jasper is linking 1 parent report with 1 child report tied with 1 or more parameters. you can configure it directly through iReport UI..so actually if you use sub report, you have to prepare at least 2 reports with 1 report act as a parent, and the other one act as a child.. after you merge it, it will be 1 single .jasper file.
sunjavastar
October 18, 2011 at 8:17 pm
How to use collection like (arraylist) as sub report in jasper report using java code
ashit
February 9, 2012 at 7:04 pm
Thanks
Abdur Rahim
February 24, 2012 at 1:10 pm
Sir i have some problem to find out the cause for which my taskReport.jasper is not run from my java code although it’s run fine with i report
my .jrxml file is as follows
I use mysql as database and then compile it through ireport and produce a .jasper file and when i call it from my java code then it produce following error
Exception in thread “AWT-EventQueue-0″ java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:469)
at net.sf.jasperreports.engine.JasperCompileManager.loadEvaluator(JasperCompileManager.java:235)
at net.sf.jasperreports.engine.fill.JRFillDataset.createCalculator(JRFillDataset.java:428)
at net.sf.jasperreports.engine.fill.JRBaseFiller.(JRBaseFiller.java:430)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:78)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:88)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:58)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:142)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:52)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:378)
at Report.createReport.importParameter(createReport.java:124)
at Report.createReport$1.actionPerformed(createReport.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
My java code is as follows
public myReport {
public static void main(String s[]){
java.util.Date SDate=new Date(“01/01/2012″);
java.util.Date EDateF=new Date(“020/02/2012″);HashMap map = new HashMap();
JasperPrint jasperPrint = null;
Connection connection = null;
String s=”%”;
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connection== DriverManager.getConnection(“jdbc:mysql://localhost/task”, “root”, “”);
map.put(“StartDate”, start);
map.put(“EndDate”, end);
map.put(“Id”,s);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(“Report/file/taskReport.jasper”);
System.out.println(path);
jasperPrint = JasperFillManager.fillReport(is, null, connection);
JRViewer jv = new JRViewer(jasperPrint);
JFrame jf = new JFrame();
jf.getContentPane().add(jv);
jf.validate();
jf.setVisible(true);
jf.setSize(new Dimension(800,600));
jf.setLocation(300,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (JRException ex) {
System.out.println(ex.getMessage());
}
}
}
Abdur Rahim
February 24, 2012 at 1:13 pm
Sir i have some problem to find out the cause for which my taskReport.jasper is not run from my java code although it’s run fine with i report
my .jrxml file is as follows
I use mysql as database and then compile it through ireport and produce a .jasper file and when i call it from my java code then it produce following error
Exception in thread “AWT-EventQueue-0″ java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:469)
at net.sf.jasperreports.engine.JasperCompileManager.loadEvaluator(JasperCompileManager.java:235)
at net.sf.jasperreports.engine.fill.JRFillDataset.createCalculator(JRFillDataset.java:428)
at net.sf.jasperreports.engine.fill.JRBaseFiller.(JRBaseFiller.java:430)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:78)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:88)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.(JRVerticalFiller.java:58)
at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:142)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:52)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:378)
at Report.createReport.importParameter(createReport.java:124)
at Report.createReport$1.actionPerformed(createReport.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
My java code is as follows
public myReport {
public static void main(String s[]){
java.util.Date SDate=new Date(“01/01/2012″);
java.util.Date EDateF=new Date(“020/02/2012″);HashMap map = new HashMap();
JasperPrint jasperPrint = null;
Connection connection = null;
String s=”%”;
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connection== DriverManager.getConnection(“jdbc:mysql://localhost/task”, “root”, “”);
map.put(“StartDate”, start);
map.put(“EndDate”, end);
map.put(“Id”,s);
InputStream is = this.getClass().getClassLoader().getResourceAsStream(“Report/file/taskReport.jasper”);
System.out.println(path);
jasperPrint = JasperFillManager.fillReport(is, null, connection);
JRViewer jv = new JRViewer(jasperPrint);
JFrame jf = new JFrame();
jf.getContentPane().add(jv);
jf.validate();
jf.setVisible(true);
jf.setSize(new Dimension(800,600));
jf.setLocation(300,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (JRException ex) {
System.out.println(ex.getMessage());
}
}
}
Hans Kristanto
February 24, 2012 at 1:18 pm
Hi Abdul Rahim,
The problem is during the compilation of the .jasper file in iReport, you use the default language of groovy. To solve this, there are 2 ways. The first way is to include groovy library so that the jasper library can look for the class. But i dont like this one. I prefer the second way, to change the default language in your report designer in iReport with Java. Go to your report designer, then look at properties, then change the default language with instead of Groovy to Java.
Hope this will help.
Thank you.