Translate

Friday, 20 December 2013

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.

Array of JavaBeans



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.

                                                                   ==The video link==



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.!!

19 comments:

  1. Thank you so much for the article. I've been struggling with ireport for days. You saved me :D

    ReplyDelete
  2. Can you please tell me if it is possible to get the java collection from HttpSession in a web application and use a '.jasper' file for design of the report to generate a pdf?

    ReplyDelete
  3. I think there is an error here i test it but there is no result

    ReplyDelete
  4. please add source to class files

    ReplyDelete
  5. Hi Ankur Gupta

    In generateCollection() method, can we get value of report parameter? and how to use it

    Thank you

    ReplyDelete
  6. Great content indeed.
    Very Useful post.
    Thanks for providing it
    โกเด้นสล็อต

    ReplyDelete
  7. Hi Ankur, Thank you for your detailed article. As you have put your data in the TestFactory.java directly, if I would like to get the data from SQL server and set the results as a javabean, will jasper report get the data?

    ReplyDelete
  8. I am interested in learning about Scriptlets and have many questions about this as well. For anyone who wants to know this, read it.


    gclub

    ReplyDelete
  9. Thanks for sharing useful article. This is most important for every users. You know about PayPal Gift Card? this is purchase card and you have more information so visit.

    ReplyDelete
  10. your article is awesome always follow your blog and found most interesting topics I also start a blog on your article is awesome always follow your blog and found most interesting topics I also start a blog on iTunes Gift card

    ReplyDelete
  11. It would be so useful blog for those are searching such kind of information.
    หวยชุดลาว

    ReplyDelete

  12. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. Kindly visit free Robux

    ReplyDelete
  13. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. If you have time you can check How to Use Google Play Credits to Buy on Amazon?.

    ReplyDelete
  14. Merci pour votre bonne volonté, votre tuto m'a été utile.

    ReplyDelete
  15. This is extremely helpful info! Everything is very interesting to learn and easy to understand. Take the given information. click Here Auto clicker

    ReplyDelete
  16. Thank you for posting such a great article! I found your website perfect essay writing service of needs. It contains wonderful and helpful posts. Keep up the good work !. Thank you for this wonderful Article! And also for great content about minecraft skin
    how to use Minecraft

    ReplyDelete

Please post your queries or suggestions here!!