Thursday, May 1, 2008

iReport Tutorial (part 4) - Calling Report With Parameter

Sometimes we need to pass one or more parameters to our report. For example, we may need to generate a report for some items with a spesific category or price. Based on our previous tutorial, we're going to make a report to view the contacts list with a certain address. For this tutorial, you have to create a new report.

Adding New Parameter

You need to specify address as a parameter, so click on View menu, then click Parameters. Click New to add new parameter. Give a name for your new parameter and specify its data type. For example, I give the name PRM_ADDRESS for this parameter and its data type is String. After that, give a check mark on Use as a prompt option. You can see the picture below for a clearer information.

Create Report Query

After you create a new parameter, you have to create report query based on that new parameter. How we do this? Click on Data menu, then click on Report Query. In the query text box, type this SQL command: SELECT * FROM contact WHERE address=$P{PRM_ADDRESS}.

As you can see the $P{PRM_ADDRESS} represents the parameter that you've made before.

After that, you have to design the report. The report's layout is up to you now. I will not talk about how to design the report here because I have talked about it in the previous tutorial.

Write Your Code

Let's write the code! Same like the previous tutorial, I'll give you all the code so you can copy-paste and modify it if necessary.



Execute It!

Final step is execute your program. Don't forget to set the CLASSPATH. If you forget how to do that, you can see the previous tutorial right before this one.

iReport Tutorial (part 3) - Calling Report From Java Application

In the previous tutorial, you've learnt how to create a report with iReport. You must be realized that the report can only be executed from iReport. But, in the real case that report should be able to be executed from the other application (usually Java application). How we do this?

Setting CLASSPATH

The first thing you have to do is set your CLASSPATH environment variable. Why? Because Java needs to load some libraries which are needed to execute that report. Those libraries are:

  • jasperreports-2.0.4.jar
  • commons-digester-1.7.jar
  • commons-logging-1.0.2.jar
  • commons-collections-2.1.jar
  • mysql-connector-java-3.1.11-bin.jar

If you are a Linux user, you can set your CLASSPATH by typing this command from your Linux console (I assume your iReport installation directory is /usr/local).

export "CLASSPATH=.:/usr/local/iReport-2.0.4/lib/jasperreports-2.0.4.jar:\
 /usr/local/iReport-2.0.4/lib/commons-digester-1.7.jar:\
 /usr/local/iReport-2.0.4/lib/commons-logging-1.0.2.jar:\
 /usr/local/iReport-2.0.4/lib/commons-collections-2.1.jar:\
 /usr/local/iReport-2.0.4/lib/mysql-connector-java-3.1.11-bin.jar"

But, if you are a Windows user, you can use set CLASSPATH=[your class path] from the command prompt utility.

Write Your Code

Let's move on! Next step is writting your Java code. This code will call the report that we've made in the previous tutorial. To make it easier, I'll give you all the code so you can copy-paste and modify it if necessary. I named this file viewReport.java, but you can use another name that you prefer.



Execute It!

Final step! Compile your Java file by typing javac viewReport.java. After that, execute it with command java viewReport. Before that, make sure your MySQL has started. And, after your program run, you can click on SHOW REPORT button to view the report.