In this example, we will be using a Java Bean Data source.
Unlike, traditional data sources, a Java bean data source is a Java class with getter and setter functions of an entity. Let us say a Person Bean has attributes like Name and Age.
Prerequisites to create a Java Bean Data source:
Eclipse or NetBeans IDE
Java Bean
Factory Class that returns the collection/array of the Beans.
Jar File creation (.Jar file)
Creating the Data Source
For this example, we use Eclipse IDE
Step 1: Create a Java Project
Call it CustomDS and click Finish.
Step 2: Add Package and add a new PersonBean class
Add Package named as “bean” under src folder and add a new PersonBean class
package bean; public class PersonBean { private String name=""; private int age = 0; public PersonBean(String name, int age) { this.name = name; this.age=age; } public int getage() { return age; } public String getName() { return name; } }
Step 3: Add javabeanset package and add TestFactory class
Add javabeanset package and add TestFactory class by importing bean.PersonBean (import bean.PersonBean;) from step 2
package javabeanset; import bean.PersonBean; public class TestFactory { //CreateArrayList public static PersonBean[] generateBeanArray() { PersonBean[] list = new PersonBean[2]; list[0] = new PersonBean("John",22); list[1] = new PersonBean("Tom",23); return list; } //createCollection public static java.util.Collection genereateCollection() { java.util.Vector collection = new java.util.Vector(); collection.add(new PersonBean("ted",25)); collection.add(new PersonBean("tom",26)); collection.add(new PersonBean("john",27)); return collection; } }
Step 4: Right-click on the Project in Eclipse and Select Export:
To create a Jar file:
Just follow the instructions on the IDE to generate a JAR file for this project.
Let us call this jar file as “CustomDSjarfile.jar”.
Step 5: Create a Java Bean Data Adapter in Jaspersoft Studio
Now, Open Jaspersoft Studio > Repository Explorer > Create Data Adapter Wizard
Name it to say “JavaBean-Array” and add the Jar file path to point to “CustomDSjarfile.jar” and then set Factory class to “Javabeanset.TestFactory” and then static method in the Factory to “generateBeanArray” and click the Test button.
As you can see generateBeanArray() is a static method created in TestFactory in Step 3.
Once information is valid, click finish to create JavaBean Data Adapter in JasperSoft Studio.
Step 6: Create a JRXML report to use this Java Bean data source.
In Jaspersoft Studio, Create a blank Jasper report; choose “JavaBean-Array” as a data source as created in the previous step.
On Dataset and Query Dialog, Select Java Bean, and search for the class name bean.PersonBean to find all the available fields for the Bean and add them to the report.
Note: Please add “CustomDSjarfile.jar” to your Project Build path via Add External JARS to search for field names.
Add name and age fields to the report.
When running a report, select “JavaBean-Array” data adapter to fill the report.
Report execution output:
Note: You can also test the Data Adapter with Collection class defined in TestFactory “genereateCollection”
Report output:
Please use this KBA as a reference only.
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now