JasperReports allows full flexibility of combining different types of data into a single report. The data can even come from multiple databases.
This recipe shows how you will display multiple types of data in a single report. You will use multiple subreports and multiple-detail sections to achieve your goal.
Refer to the installPostgreSQL.txt
file included in the source code download of this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code of this chapter also includes two files named copySampleDataIntoPGS.txt
and copyCustomerHistorySampletDataIntoPGS.txt
. The copySampleDataIntoPGS.txt
file will help you create a database named jasperdb6
and create a table named CustomerInvoices
with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data for this recipe. Similarly, the copyCustomerHistorySampletDataIntoPGS.txt
file will help you create a database named jasperdb6a
and create a table named CustomerHistory
with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName
, and InvoiceValue)
and copy sample data.
You will be using three JRXML files MultipleData.jrxml, CustHistorySubreport.jrxml
, and ProductInvoices.jrxml
in this recipe. You will find these files in the Task6
folder of the source code download of this chapter. The MultipleData.jrxml
file is the master report, which uses the other two files as subreports. The master report has to refer to all of its subreports using a complete path (you cannot use relative paths). This means you have to copy the three JRXML files to the C:JasperReportsCookBookSamples
folder in your PC. I have hardcoded this complete path in the master report (MultipleData.jrxml).
MultipleData.jrxml
file from the C:JasperReportsCookBookSamples
folder of your PC. The Designer tab of iReport shows a report with data in the Title and Customer Group Header 1 sections, as shown in the following screenshot: CustHistorySubreport.jrxml
file placed in the C:JasperReportsCookBookSamples
folder of your PC and press the Open button, as shown in the next screenshot. The browse dialog will disappear. Press the Next button in the Subreport wizard dialog. java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/jasperdb6a", "postgres", "postgres")
expression in the area below the Use another connection option, as shown in the following screenshot. Press the Next button at bottom of the window. InvoiceID
and CustomerName
parameters of the CustHistorySubreport.jrxml
subreport and allows you to enter expressions to map these to a number of elements (that is, Fields or Variables) of the main MultipleData.jrxml
report. CustomerName
parameter. A drop-down list will open. This will show all elements of the MultipleData.jrxml
report. Select CustomerName Field
from the list, as shown. Press the Next button. 80
. 15
. ProductInvoices.jrxml
file placed in the C:JasperReportsCookBookSamples
folder of your PC and press Open button. The browse dialog will disappear. Press the Next button in the Subreport wizard dialog. CustomerName
parameter of the ProductInvoices.jrxml
subreport and allows you to enter expressions to map these to a number of elements (that is, Fields or Variables) of the main MultipleData.jrxml
report. CustomerName
parameter. A drop-down list will open. This will show all elements of the MultipleData.jrxml
report. Select CustomerName Field
from the list. Click the Next button. 80
. 15
.You have learned to combine different types of data from different sources into a single report. For this purpose, you inserted two subreports into a master report. Each of your subreports uses its own database connection, which you authored in step 6 for your first subreport and step 19 for your second subreport.
Note that the first subreport uses a database connection that is different from the master report's database connection. This is why you chose the Use another connection option in step 6.
The second subreport uses the same database connection as the master report. So you chose the Use the same connection used to fill the master report option in step 19.
Also notice that you passed the CustomerName
field as parameter to the first subreport in steps 7 and 8. Similarly, you passed the same CustomerName
field as parameter to the second subreport in steps 20 and 21. This way, both subreports provide their specific types of data for a specific customer at a time. The master report calls each subreport for different customers, one at a time, and organizes the presentation of all data in a single multi-page report.
3.14.144.229