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

21 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. 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
  7. 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
  8. 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
  9. 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
  10. It would be so useful blog for those are searching such kind of information.
    หวยชุดลาว

    ReplyDelete

  11. 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
  12. 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
  13. Merci pour votre bonne volonté, votre tuto m'a été utile.

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

    ReplyDelete
  15. Your blog is very fantastic. It inspire me lot and also easy to understand. Thanks for this great article. I am a gamer and I also have something for you. you can check here

    ReplyDelete
  16. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses click here
    Check here

    ReplyDelete
  17. Found your blog pretty amazing. I really liked the idea of this post. Keep up the good work. I also have something for you to Compress and archive files and data. You can check Winrar.

    ReplyDelete
  18. What a brilliant job you’ve accomplished here. I read few posts on this web site and I believe that your blog is real interesting and holds circles of wonderful information. I also have something for you to Compress and archive files and data. Winrar
    visit here

    ReplyDelete
  19. I think this is among the most vital information for me. And I'm glad to read your article. Also, I am glad to share a brilliant post about BSL Shaders 1.14.4 for minecraft. I hope you will really enjoy this shader pack. You can Download BSL Shader from here.

    ReplyDelete
  20. Awesome article! It is in detail and well-formatted that I enjoyed reading. which in turn helped me to get new information from your blog. I am sharing a blog that is important for gamers click on game cube

    ReplyDelete

Please post your queries or suggestions here!!