Usually I prefer use pure SQL as datasource for various database-related tasks, e.g. reporting. But for some tasks is better way to use JavaBean as datasource model. Below I will write sample such datasource and reveal how use it for obtain such result.
|Picture 1: Report, based on JavaBean datasource.|
Create JavaBean datasource.
Below we will simulate master and detail tables. For master table we will use model of order documents; for detail table we will use model of records, related with master. And, at last, we will incorporate these models in factory which return data for iReport.
I advice create jar file for have our datasource as one file . Also you can download my jar-file.
Creating main reports.
Before creating reports we must register our datasource. It include two clauses:
- Add datasource to CLASSPATH. In iReport case open "Tools/Options" and add class or jar files in appropriate place.
|Picture 2: Set CLASSPATH|
- For register datasource press Report Datasourced button. Next press New button and select JavaBeans set datasource item. In next window fill all fields as on the picture:
|Picture 3: Set datasource properties|
- Now we can create report. In new report disable all bands except Detail and save as java_beans_datasource_report.jrxml.
- Next we must register our datafields. Call "Edit query" and fill parameters as on the picture:
|Picture 4: Set datafields|
- Add Ndoc and Date fields on Detail band:
|Picture 5: Adding fields on main report|
If you did everything correctly you can see next result:
|Picture 6: Our first result.|
Add subreport component from Pallete and save it as java_beans_datasource_report_subreport1.jrxml. Next, call edit query and fill parameters as on the picture:
|Picture 6: Set datafields for subreport.|
In subreport disable all bands except Detail and Title. For me it looks like below:
|Picture 7: Mastering subreport.|
Now need tune subreport datasource. Switch to main (master) report and select Subreport component on Detail band. Set subreport properties:
Last step it save and compile subreport and main report. Press Preview button and enjoy result from Picture1.
- jar-file with JavaBean datasource.
- Master report java_beans_datasource_report.jrxml Note: perhaps you need to change Default Value Expression of SUBREPORT_DIR parameter to other, than "./". Set it corresponding to directory what contains jrxml files.
- Subreport java_beans_datasource_report_subreport1.jrxml
- Source code (include all jar, java and jrxml files).
- Source code from BitBucket repository.
- Runing JasperReport subreport from java code tell you about running this sample from java code.
- JasperReport and Maven for creating distributive