Jump to content
Changes to the Jaspersoft community edition download ×
  • Working with JavaBeans Data Adapter in TIBCO Jaspersoft® Studio


    Tirupathi Mangalarapu
    • Features: Custom Data Sources, Data Sources Version: v7.1.0, v6.4.3 Product: Jaspersoft® Studio

    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.

    step1_0.jpg.a9230d2eddf85c945df941c1c056ad7f.jpg

    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:

    step4.jpg.2507e4f52ee88a04a7f1cfcd170587e2.jpg

    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.

    step5.jpg.86b95d0255642cae064189ae0b9ef141.jpg

    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.

    step6a.jpg.f2113acba0a487a24898bb0637396f4f.jpg

    step6b.jpg.e660a5228845bd3c81b262d28ee282db.jpg

    Add name and age fields to the report.

    When running a report, select “JavaBean-Array” data adapter to fill the report.

    step6c.jpg.c841f50520beb3ee6541f5efa7124148.jpg

     Report execution output:

    step6d.jpg.4a3250c3c2cc6353ee4c759661005ca4.jpg

    Note: You can also test the Data Adapter with Collection class defined in TestFactory “genereateCollection”

    step6e.jpg.00c651de85aecb22b85a722f5f0d278c.jpg

    Report output:

    step6f.jpg.5c62808a1b06a8e98e201e998f759673.jpg

    Please use this KBA as a reference only. 


    References:

    customdsjarfile.jar

    javabean-array.xml

    javabean-collection.xml

    customds.zip

    javabeandatasource_example.jrxml


    User Feedback

    Recommended Comments

    Fine but there is one important point missing here:

    The example provided here is applicable only if the report is in the same workstation with java appcation (the jasper report file .jrxml/.jasper is bundled with the web application, deployed in the same web/app server). But what if the report is depoyed in a remote jasper report server and not in the same server with the bundled web app? How do you pass the ArrayList of beans to the jasper file in such a case?

    Thanks.

    Link to comment
    Share on other sites


    I tried your instructions, but as a beginner with Jasper Reports I got lost. It would be better if you took more time and added a picture for each step. Shortening the procedure is useless in any tutorial. I appreciate your efforts, but it didn't give me anything. I spent a lot of time on it and it was no use.

    Link to comment
    Share on other sites

    Hello Zikacek17,

    I am sorry to hear that you could not get that working. If you are getting any errors and not using Community Edition of JRS then please work with our support team to resolve it.

    Thank you

    Jaspersoft Team 

    Link to comment
    Share on other sites

    Hi,

    I have requirement where Jasper should be able to send parameters to Java Adapter and Java method will call the StoredProcedure and get the list of records, then my java method will arrange/modify the records as per my requirement, so kindly let me know how this can be achieved.

    Link to comment
    Share on other sites



    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 account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...