So now, you’ve seen the 2 previous articles. And now, let’s move on the core of JasperReport and the integration of it to the desktop application. I guarantee you that you really need this article because you don’t want your application only capable to do just System.out.println(“Hello Everybody!”); do you?
Let’s get it on!
1. Building your report
You can see in the right panel, there’s a palette panel. If you can’t see it, go to Window -> Palette to activate palette panel. Drag a static text to the Title Section as follows :
After that, insert column title into similar like this :

Note : You can add additional line, text, etc as you like.
2. Integrate it with Query
Click on the button with this icon : 
You will see a query window. Type this query on the text area (note that this is SQL query, not Hibernate query, JPQL, etc.)
SELECT employee_id AS "E_ID", employee_name AS "E_NAME", salary AS "E_SALARY" FROM employees
Let’s analyze the code above. I’m sure that all of you must be familiar with this code. But one thing you have to know that, in JasperReport, ALIAS will be converted automatically into FIELDS. In this case, you have 3 aliases, E_ID, E_NAME, and E_SALARY. All of these will be converted into JasperReport FIELDS Variable, so that you can place these elements in this design.
Next, place these 3 elements into your design (Dont forget, put these in Details Section), just simply like this :
3. Testing your report
Click on preview button in the toolbar. If you work in the right way, you will see display like this :
The report works fine. Remember, whenever you clicked on Preview, the iReport will automatically compile the report from .jrxml into .jasper. The .jasper file is the generated object that you can use it and directly integrate it with the application.
Note : for iReport 3.7.0 user, you have to change the default language from Groovy to Java. Right click on your report name node in the left side, choose properties, and change the language from Groovy to Java.
Let’s move on to Netbeans.
4. Create a new Java Application Project and Insert JasperReport library
Create the java application first. Uncheck the Create Main Class option. After that, add JasperReport library and MySQL JDBC Driver to your project (Right click on the library node of your project, add new library)as follow :
Add JasperReport library & MySQL JDBC Driver on it. You will see like this once you get done :
Next, create 2 package in your Source Packages, 1 package to place your report, the other one to place your main application. Give it name :
Move your .jasper file into your report package.
Insert a main class into your application package.
5. Code your application
Open your main class, and insert this code to your class (i will explain it component per component) :
package id.hans.employee.application;
import java.awt.Dimension;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;
/**
*
* @author Hans Kristanto
*/
public class MainApplication {
Connection conn = null;
public void initConnection(){
String HOST = "jdbc:mysql://localhost:3306/DATABASE_NAME";
String USERNAME = "YOUR_MYSQL_USERNAME";
String PASSWORD = "YOUR_MYSQL_PASSWORD";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
conn = DriverManager.getConnection(HOST, USERNAME, PASSWORD);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void showReport(){
//Path to your .jasper file in your package
String reportName = "id/hans/employee/report/EmployeeReport.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
JRViewer jv = new 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(800,600));
jf.setLocation(300,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (JRException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
MainApplication ma = new MainApplication();
ma.initConnection();
ma.showReport();
}
}
If no problem occured, you will see display like this :
Congratulations! you’ve created your first JasperReport application!











guest
March 17, 2010 at 3:02 pm
nice tutorial…well structured and also with clear example…wheew…i wish i found this tutorial 2 years ago…hehe
izzid
March 29, 2010 at 12:34 pm
thank …
iddev
April 19, 2010 at 12:52 pm
Thank you, how do I insert an SQL Query in java file not in the report ?
Hans Kristanto
May 19, 2010 at 1:25 pm
@iddev
you can add an SQL query, but you can’t compile it first into .jasper file.. let you copy your .jrxml file into your project, add your SQL query, and then everytime you run it, the application should re-compile the report.
siva
June 16, 2010 at 12:05 pm
Really this article helps for new users who is working on jasper reports.Thanks for the Author.good
Rushi
June 17, 2010 at 5:20 am
Hi,
I tried creating a simple report for practice using this tutorial.
After following ’2. Integrate it with Query’ step of this tutorial when I tested the report by clicking on preview button, I am getting only first record in a given table.
Note that I have number of record in my employee table, still I got only 1 record.
Can you help me with this?
Havn
July 26, 2010 at 9:09 am
hi ,
i want change sql query in .jrxml . How can i do ?
shohan
October 25, 2010 at 12:15 pm
Thanks
David
January 6, 2011 at 3:04 am
Hi!..
I triying to make a report but i wanto to use a jpql expression. what can i do ?
Thanks.
LightVision
August 29, 2011 at 10:45 pm
Hi,
Your tutorial are great, thank you!
Unfortunately is not well indexed. I was struggle-ling 2 weak before finding it.
Nice and clever!
Shean
August 30, 2011 at 3:54 pm
How to make I report (jasper) sub report….Plz tel me…
josel
October 9, 2011 at 3:53 am
i follow your steps. why is it this error occurs?—please help me..
net.sf.jasperreports.engine.JRException: Error loading object from InputStream
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:198)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:376)
at id.hans.employee.application.Main.showReport(Main.java:50)
at id.hans.employee.application.Main.main(Main.java:72)
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.(ObjectInputStream.java:279)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.(ContextClassLoaderObjectInputStream.java:53)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:193)
… 3 more
i added the following libraries in my netbeans libraries///
Hans Kristanto
October 9, 2011 at 7:33 am
Try to check the path for ur .jasper file. This might be due to this.
Hans Kristanto
josel
October 9, 2011 at 1:54 pm
i chek the path of my .jasper file already..what do you mean in this line
//Fill the report with parameter, connection and the stream reader
52
JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
is netbeans 6.9.1, ireport 4.1.2 and your libraries are compatible…thank Hans
Hans Kristanto
October 9, 2011 at 5:00 pm
the line means, u filling out the report with the inputStream (described as is variable) with the connection that later on can be used to generate the report with the pre-configuration query that u defined. i suspected that your jasperreport version and your ireport version does not match each other. try to download the latest version of jasperreport and as well as the ireport. thanks.
josel
October 9, 2011 at 5:13 pm
i tried to use the same netbeans version which is netbeans 6.7.1 and irepot 3.7.0 and the same libriries you used..still that error occured..what’s wrong? thanks..
Hans Kristanto
October 9, 2011 at 5:16 pm
please check the jasperreport version.. this might be mismatch between the jasperreport and ireport version.. netbeans i think should be ok, no issue for this..
josel
October 9, 2011 at 5:37 pm
i used the latest version of ireport which is 4.1.2 and jasperreport which is 4.1.2 also..but the error still occurs..
run:
net.sf.jasperreports.engine.JRException: Error loading object from InputStream
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:215)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:376)
at id.hans.employee.application.Main.showReport(Main.java:50)
at id.hans.employee.application.Main.main(Main.java:79)
Caused by: java.io.StreamCorruptedException: invalid stream header: 3C3F786D
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.(ObjectInputStream.java:279)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.(ContextClassLoaderObjectInputStream.java:53)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:210)
… 3 more
BUILD SUCCESSFUL (total time: 1 second)
that error always pointing to this line
JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
please do help me…
Hans Kristanto
October 9, 2011 at 6:38 pm
it will pointing to that line because it failed to generate the InputStream from your .jasper file.. please send your project file to me and i will try to check it..
josel
October 9, 2011 at 7:11 pm
Hans where i will send my project?what is your email. thank you
josel
October 9, 2011 at 10:07 pm
Hans,
Thank you..I already solved my problem..I install the ireport plugins in netbeans…
Hans Kristanto
October 9, 2011 at 10:33 pm
Great! Glad to hear that
Hans Kristanto
Rudolf Damanik Gamelan
October 12, 2011 at 12:54 pm
hei. Josel. I have a same problem like yours. Can you tell me how to solve that problem? Because I’m in deadline of presentation of my program that using JasperReport. Thank you so much.
Sam
October 27, 2011 at 1:13 am
can u pls tell me why do u use this line
//Get a stream to read the file
InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);
Shekhar Kadam
November 9, 2011 at 1:51 pm
Thank you so much…………..
duong
November 29, 2011 at 3:11 am
Hi Hans,
I have a problem, when I click on button print :
My PHP1:
try {
String reportName1 = “/DHDL/Quanlydiem/Report/report1.jrxml”; // In src folder.
JOptionPane.showMessageDialog(this,reportName1);
Connection conn = Config.GetConnection();
//InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName1);
InputStream is1=getInputStream(reportName1);
//JOptionPane.showMessageDialog(this,is); //Test is
// JOptionPane.showMessageDialog(this,is1);//Test is1
// JOptionPane.showMessageDialog(this,conn);
JasperPrint jasperPrint = JasperFillManager.fillReport(is1, null, conn);
//JOptionPane.showMessageDialog(this,jasperPrint); //test jasperprin
JasperViewer jv = new JasperViewer(jasperPrint, false);
jv.setVisible(true);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Or PHP:
try {
ConfigDB Config1 = new ConfigDB();
JasperReport reporter = JasperCompileManager.compileReport(“report1.jrxml”); // In Quanlydiem folder.
JasperPrint print=JasperFillManager.fillReport(reporter, null,Config1.GetConnection());
JasperViewer.viewReport(print);
} catch (JRException ex) {
System.out.println(ex.getMessage());
}
then have error :
Null
or
No report compiler set for language : null
I think: JasperPrint print=JasperFillManager.fillReport(reporter, null,Config1.GetConnection()); ———->Null.
but I still not found this error.
pls, help me.
thank you
Siddhesh Velangi
January 2, 2012 at 5:44 pm
Could you please let me knoe where can i find a good tutorial how to use avanced componets like sort, table. I am unable to find them.