Using multiple relational databases to generate a report

This recipe teaches you how to create a report with data coming from two different database sources. The main report has six columns, five of which come from one database. The sixth column comes from a subreport, which fetches its data from the second database.

Getting ready

Refer to the installPostgreSQL.txt file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed.

The source code for this chapter also includes two files named copySampleDataIntoPGS.txt and copySamplePaymentStatusDataIntoPGS.txt. The copySampleDataIntoPGS.txt file will help you to create a database named jasperdb5 and create a table named CustomerInvoices with five columns (InvoiceID, CustomerName, InvoicePeriod, ProductName, and InvoiceValue) and copy sample data for this recipe. Similarly, the copySamplePaymentStatusDataIntoPGS.txt file will help you to create a database named jasperdb5a and create a table named PaymentDetails with two columns (InvoiceID and PaymentStatus) and copy sample data.

You will be using two JRXML files MultiDBReport.jrxml and PaymentStatusSubreport.jrxml in this recipe. You will find these files in the Task4 folder of the source code download for this chapter. The MultiDBReport.jrxml file is the master report, which uses the other file as a subreport. The master report has to refer to its subreport using a complete path (you cannot use relative paths). This means you have to copy the two JRXML files to the c:JasperReportsCookBookSamples folder on your PC. I have hardcoded this complete path in the master report (MultiDBReport.jrxml).

How to do it...

  1. Open the PaymentStatusSubreport.jrxml file from the c:JasperReportsCookBookSamples folder. The Designer tab of iReport shows an empty report, as shown in the following screenshot:
    How to do it...
  2. Right-click on the Parameters node in the Report Inspector window on the left of the Designer tab, as shown next. Choose the Add Parameter option from the pop-up menu.
    How to do it...
  3. The Parameters node will expand to show the newly added parameter named parameter1 at the end of the parameters list. Click on parameter1, its properties will appear in the Properties window below the palette of components on the right of your iReport main window.
  4. Click on the Name property of the parameter and type InvoiceID as its value. The name of the parameter1 parameter will change to InvoiceID.
  5. Click on the Parameter Class property and select java.lang.Integer as its value.
  6. Click on the Default Value Expression property and enter 0 as its value, as shown in the following screenshot. Leave the rest of the parameter properties at their default values.
    How to do it...
  7. Click the Report query button on the right of the Preview tab; a Report query dialog will appear, as shown in the following screenshot:
    How to do it...
  8. Type SELECT * FROM paymentdetails WHERE invoiceid = $P{InvoiceID} in the Query editor. The fields of the paymentdetails table will be shown in the lower-half of the Report query dialog. Click the OK button, as shown in the following screenshot:
    How to do it...
  9. Double-click the Fields node in the Report Inspector window. You will see that it contains invoiceid and paymentstatus fields, as shown in the following screenshot:
    How to do it...
  10. Drag-and-drop the paymentstatus field from the Fields node into the top-left corner of the Detail 1 section, as shown in the following screenshot:
    How to do it...
  11. Select PaymentDetails in the datasources drop-down list, as shown in the left image given below. Then switch to the Preview tab; a Parameter prompt dialog will appear, which will ask you for the invoice ID, as shown in the right image given below. Enter 1001 as the value of the InvoiceID parameter. You will see a report containing a single record showing the payment status of the invoice having the ID 1001.
    How to do it...
  12. Switch back to the Designer tab. Click anywhere in the Page Header section; its properties will appear in the Properties window below the palette. Select the Band height property and set 0 as its value, as shown in the following screenshot:
    How to do it...
  13. Similarly, repeat step 12 for the Title, Column Header, Column Footer, Page Footer, and Summary sections and set 0 as the value of the Band height property. This will set all sections to zero height except the Detail 1 section.
  14. Double-click on the blue line at the bottom of the Detail 1 section. The height of the Detail 1 section will become equal to the height of the text field dropped into the Detail 1 section in step 10. The report in the Designer tab will look as shown in the following screenshot:
    How to do it...
  15. Click on the name of your report in the Report Inspector window on the left of the Designer tab, as shown in the following screenshot:
    How to do it...
  16. The report properties will appear in the Properties window below the palette. Set 0 as the value of the Top margin and Bottom margin properties in the Margins section of the Properties window, as shown in the following screenshot:
    How to do it...
  17. Now open another report named MultiDBReport.jrxml from the C:JasperReportsCookBookSamples folder. The Designer tab of iReport shows a report containing data in the Title, Column Header, Customer Group Header1, and Detail 1 sections, as shown in the following screenshot:
    How to do it...
  18. Select PG in the datasources drop-down list. Then click the Preview button and you will see a report containing invoices grouped by customer names, as shown in the following screenshot:
    How to do it...
  19. Copy-paste the Invoice Value column label and place the new copy of the Invoice Value label to the right of the existing label, as shown in the following screenshot:
    How to do it...
  20. Double-click on the new copy of the Invoice Value label and type Payment Status as its value.
  21. Drag-and-drop a Subreport component from the palette into the Detail 1 section, just below the PaymentStatus label. A Subreport wizard dialog will appear, as shown in the following screenshot:
    How to do it...
  22. Choose the Use an existing report option and click the Browse button to browse to the PaymentStatusSubreport.jrxml file located in the c:JasperReportsCookBookSamples folder and click the Open button, as shown next. The browser dialog will disappear. Click the Next button in the Subreport wizard dialog.
    How to do it...
  23. A Connection exp (2 of 4) dialog will appear, as shown in the following screenshot. Notice that now the left side of the Subreport wizard shows four steps (Subreport,Connection exp, Parameters, Subreport exp). You are at step 2 (Connection exp) which is shown in bold.
    How to do it...
  24. Choose the Use another connection option and type the java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/jasperdb5a", "postgres", "postgres") expression in the area below the Use another connection option, as shown next. Click the Next button at the bottom of the window.
    How to do it...
  25. A Parameters (3 of 4) dialog will appear. This will show the InvoiceID parameter of the PaymentStatusSubreport.jrxml subreport and allows you to enter an expression to map it to elements (that is Fields or Variables) of the main MultiDBReport.jrxml report.
    How to do it...
  26. Click on the Expression field beside the InvoiceID parameter. A drop-down list will open. This will show all elements of the MultiDBReport.jrxml report. Select the InvoiceID field from the list, as shown in the following screenshot:
    How to do it...
    • Click the Next button.
  27. A Subreport exp (4 of 4) dialog will appear. Continue with the Store the directory name in a parameter option selected and click the Finish button.
    How to do it...
  28. The Subreport element will be placed in the Detail 1 section, as shown in the following screenshot:
    How to do it...
  29. Click on the Subreport element; its properties will appear in the Properties window below the palette. Find the Width property and set 80 as its value.
  30. Click on the Height property and set 15 as its value, as shown in the following screenshot:
    How to do it...
  31. The size of the Subreport element will become equal to the size of the other fields placed in the Detail 1 section, as shown in the following screenshot:
    How to do it...
  32. Click the Preview button and you will see a report containing invoices with their status coming from the payment status subreport, as shown in the following screenshot:
    How to do it...

How it works...

In this recipe, you have done four things:

  1. First you made a subreport with a parameter named InvoiceID in steps 1 to 16. The subreport is a very useful feature that allows you to design a portion of a report as a separate, independent report and then insert the portion into the main report.
  2. Then you inserted the subreport into a main report in step 21.
  3. You configured a new database connection for the subreport in step 24. Note from the screenshot of step 24 that you used the java.sql.DriverManager class to configure this database connection for the subreport.

    When I configured the database connection in step 24, the name of my database server was postgresql, the network address of the machine hosting the server was localhost, the server was listening at port 5432, the name of the database was jasperdb5a, and the username and password were both postgres. Putting all this together, I got the complete expression of step 24 as "java.sql.DriverManager.getConnection ("jdbc:postgresql://localhost:5432/jasperdb5a", "postgres", "postgres")". This way you can configure any database (for example MySQL) for your subreport.

  4. You mapped the InvoiceID parameter of the subreport to the InvoiceID field of the main report in steps 25 and 26.

The result of these four steps is that the main report fetches its five columns from its own database connection. Then it asks the subreport to provide data for the sixth column (that is, Payment Status). The subreport fetches data for the sixth column from another database connection and returns the data to the main report, which eventually displays the data coming from the subreport in the sixth column.

This way you can design reports using multiple databases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.204.0