Jump to content

Using JavaBeans as datasource


olu_guy

Recommended Posts

Hi All, this question has been asked several times on this forum and other forums but unfortunately there's been no post (s) that actually provides a proper example or solution.

 

I am trying to use javabeans as the datasource for my jasper reports. Does anyone have an example on how to implement this step by step ? I have seen some examples but none of them addresses the problem or provide a step by step guide.

 

If there links to tutorials on using javabeans as a datasource in ireports or jasper reports that will be fully appreciated.

 

Cheers.

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

To use java beans is fairly straight forward. Just create either a collection or an array of the bean object and encapsulate that in either a JRBeanArrayDataSource or JRBeanCollectionDataSource. Just make sure the object your encapsulating adheres to the beans standard...

1. No public visibility fields

2. Properly named getter/setter methods.

3. Default no-args c-tor

 

To fill your report, you would have something like...

 

 

 

Code:
	ArrayList al = new ArrayList();
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));
al.add(new SomeBean("SomeParam"«»));

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(al);


JasperReport jasperReport;
JasperPrint jasperPrint;
try
{

jasperReport = JasperCompileManager.compileReport(ROOT_DIR + "myReport.jrxml"«»);
Map customParameters = new HashMap();

jasperPrint = JasperFillManager.fillReport(
jasperReport, customParameters, ds);

JasperExportManager.exportReportToPdfFile(
jasperPrint, "c:\test_output.pdf"«»);

}
catch (JRException e)
{
e.printStackTrace();
}

 

HTH,

Robin

Link to comment
Share on other sites

Thanks robin, I will implement the solution using the guide as provided by you. I dont thnik I will run into problems. That looks to be simple and easy and not complicated as I first thought. I'll write up a manual afterwards so people can benefit from such an example.

 

Cheers.

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