Jump to content
Changes to the Jaspersoft community edition download ×
  • How to set JavaBean set as Datasource in iReports, along with a working sample.


    Java beans are practically, classes that encapsulate many objects into a single object (the bean). This is pretty simple to create and could easily hold lots of data in them. They are serializable, have a 0-argument constructor, and allow access to properties using getter and setter methods.

    In a nutshell, java bean is easy to use methodology to obtain data. Now the task in hand is that how to use this bean in our reports . For setting the bean in reports there are few prerequisites, 

    1. There should be a Bean class
    2. There should be a factory class which returns the collection/array of the beans
    iReport provides two options, either it could read from collection of beans or array of beans. First of all we need a bean, so below is how the bean looks like.
    /**
    * Bean
    *
    * @author Ankur Gupta
    */
    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;
    }
    }

    Now you need to create a factory class which contains the function which returns the bean.There are two ways to do that :
    1. Collection of bean

    /**
    * Responsible for filling values in the Bean and return it as collection of bean.
    *
    * @author Ankur Gupta
    */
    public class TestFactory {

    public static java.util.Collection generateCollection() {
    // Creates the collection
    java.util.Vector collection = new java.util.Vector();

    // Adds the values in the bean and adds it into the collection
    collection.add(new PersonBean("Ted", 20));
    collection.add(new PersonBean("Jack", 34));
    collection.add(new PersonBean("Bob", 56));
    collection.add(new PersonBean("Alice", 12));
    collection.add(new PersonBean("Robin", 22));
    collection.add(new PersonBean("Peter", 28));

    // returns the collection of beans.
    return collection;
    }
    }
    2. Array of Beans

    /**
    * Responsible for filling values in the Bean and return it as collection of bean.
    *
    * @author Ankur Gupta
    */
    public class TestFactory {

    // Creates the Arraylist
    PersonBean[] list = new PersonBean[6];

    // Adds the values in the bean and adds it into the Array
    list[0]= (new PersonBean("Ted", 20));
    list[1]= (new PersonBean("Jack", 34));
    list[2]= (new PersonBean("Bob", 56));
    list[3]= (new PersonBean("Alice", 12));
    list[4]= (new PersonBean("Robin", 22));
    list[5]= (new PersonBean("Peter", 28));

    // returns the Array of beans.
    return list;
    }
    }

    Now, we need to compile these files  and put the generated jar file in the classpath of the iReport.

    Next we need to set the datasource, by adding datasource Java Bean set as datasource and fill in the Factory class as well as the function which generates the bean as array or collection, whichever suits our purpose.

    In this case:
    Factory class  :  javabeanset.TestFactory
    Generating function: generateBeanArray/generateCollection (based upon collection or array)
    Below are some screen shots.

    http://1.bp.blogspot.com/-kRCYrv4FigE/UrQk3qR8K9I/AAAAAAAAAYw/bH2zTQQcZHk/s400/Untitled.png
    Array of JavaBeans



    http://4.bp.blogspot.com/-wtHdWNEB30o/UrQlOM5YsNI/AAAAAAAAAY4/yXihZsrIWmU/s400/Untitled.png
    Collection of JavaBeans

    You can follow the video link for setting javabeans as datasource.  Here is the Youtube video made by me which gives a complete walkthrough to set the datasource as javabean with the help of a sample report also.

                                                                       




    I have made a sample in the video itself here is zip file which contains:
    1, Jar File (You need to place this in your classpath)
    2. JRXML File
    3. Set datasource as shown in the video and insert the values provided in the above screenshots.
    4. Test Run PDF(PDF output of the Sample report)
                                                                == ZIP Sample File ==
    Follow all the steps in order to get the sample working.


    Hope this clears your doubt for this topic. Still if you face any issues or problems then you can add a comment below .

    Happy Coding.!!


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...