Getting started with JavaBean collection datasource in Jasper Reports

I'm new to Jasper Reports.I was able to generate a report successfully with SQL query embedded inside the jrxml file. (I'm using JasperStudio plugin for eclipse)

But now I want to use my bean class as a datasource. Need some help in getting started -
Requirement : User-enters values in the Donation Receipt form on screen. Persist DonationReceipt bean in the database and generate a Donation receipt pdf with the bean values.

1. Created a new Data Adapter of "Collection of JavaBeans" type -

Select Adapter type Created a new Data Adapter

  1. I created a static method in my bean class "createBeanCollection" as mentioned in the Data Adapter Wizard.
    enter image description here
    Most references on the internet show hard-coded values being set into the bean, within the createBeanCollection() method. How do I use my bean with values determined at runtime instead of hardcoding.?

  2. I need to generate multiple receipts and reports in my application, each mapping to a different bean. e.g. WeeklyRevenueReport, DonationReceipt etc.
    Do I need to create a data adapter for each one? Or a single adapter can be used for multiple types of report generation using different bean types?

Upvotes: 0

Views: 978

Answers (1)

amdg
amdg

Reputation: 2239

We achieved it using the code similar to the snippet below.

public static Collection<DonationReceipt> createBeanCollection(){
  List<DonationReceipt> dataList = new ArrayList<DonationReceipt>();
  String sqlQuery = '<your select SQL>';
  ...
  ResultSet rs = ..
  while(rs.next()){
     DonationReceipt dr = new DonationReceipt();
     dr.setReceiptNo(rs.getString(..));
     ...
     dataList.add(dr); 
  }
  return dataList;

Upvotes: 1

Related Questions