Creating a database report in record time
iReport contains a wizard that allows us to quickly generate database reports (very
useful if the boss asks for a report 15 minutes before the quitting time on a Friday!).
The wizard allows us to use one of the predefined templates that are included with
iReport. The included report templates are divided into two groups: templates laid
out in a “columnar” manner and templates laid out in a “tabular” manner. Columnar
templates generate reports that are laid out in columns, and tabular templates
generate reports that are laid out like a table.
In this section, we will create a report displaying all the aircraft with a horsepower
of 1000 or more.
To quickly create a database report, we need to go to File | New | Report Wizard.
We should then enter an appropriate name and location for our report and click
on Next>.
Next, we need to select the datasource or database connection to use for our report.
For our example, we will use the JDBC connection we configured in the previous
section. We can then enter the database query we will use to create the report.
Alternatively, we can use the iReport query designer to design the query.
For individuals with SQL experience, in many cases it is easier
to come up with the database query in a separate database client
tool and then paste it in the Query text area than using the
query designer.
The complete query for the report is:
select
a.tail_num,
a.aircraft_serial,
am.model as aircraft_model,
ae.model as engine_model
from aircraft a, aircraft_models am, aircraft_engines ae
where a.aircraft_model_code = am.aircraft_model_code
and a.aircraft_engine_code = ae.aircraft_engine_code
and ae.horsepower >= 1000
The following window shows a list of all the columns selected in the query, allowing
us to select which ones we would like to use as report fields:
In this case, we want the data for all columns in the query to be displayed in the
report. Therefore, we select all columns by clicking on the second button.
We then select how we want to group the data and click on Next>. This creates a
report group. (Refer to the Grouping Report Data section in Chapter 6, Report Layout
and Design for details.)
In this example, we will not group the report data. The screenshot illustrates how
the drop-down box contains the report fields selected in the previous step.
We then select the report layout (Columnar or Tabular). In this example, we will
use the Tabular Layout.
After selecting the layout, we click on Next> to be presented with the last step.
We then click on Finish to generate the report’s JRXML template.
While the template is automatically saved when it is created, the
report generated by the Preview button is not automatically saved.
We can then preview our report by clicking on Preview.
That’s it! We have created a report by simply entering a query and selecting a few
options from a wizard.
Tweaking the generated report
Admittedly, the report title and column headers of our report need some tweaking.
To modify the report title so that it actually refl ects the report contents, we can either
double-click on the report title on iReport’s main window and type an appropriate
report title, or we can modify the value of the Text property for the title static text in
the Properties window at the lower righthand side.
Double-clicking on the title is certainly the fastest way to modify it. However, the
Properties window allows us to modify not only the text, but also the font, borders,
and several other properties.
We can follow the same procedure for each column header. The following screenshot
shows the resulting template as displayed in iReport’s main window:
We’ll preview the report one more time to see the final version.
There you have it! The boss can have his or her report, and we can leave work and
enjoy the weekend!
Creating a report from scratch
In the previous section, we discussed how to quickly generate a database report by
using iReport’s Report Wizard. The wizard is very convenient because it allows us to
create a report very quickly. However, its disadvantage is that it is not very fl exible.
In this section, we will learn how to create a report from scratch in iReport. Our
report will show the tail number, serial number, and model of every aircraft in the
FlightStats database.
To create a new report, we need to go to the File | New | Empty report menu item.
At this point, we should enter a Report name and Location.
In this example, we will set the report name to iReportDemo and accept all the other
default values. After clicking on the OK button, iReport’s main window should look
like this:
The horizontal lines divide the different report sections. Any item we insert between
any two horizontal lines will be placed in the appropriate report section’s band.
Horizontal lines can be dragged to resize the appropriate section(s).
The vertical lines represent the left and right report margins. It is not possible to drag
the vertical lines. To modify the left and right margins, we must select the report in
the Report Inspector window at the top left.
Then, we need to modify the margins from the Properties window at the bottom right.
Properties for all the report sections and elements, such as variables,
scriptlets, title, background, detail, and so on, can be modified by
following the approach described here.
Going back to our empty report template, let’s add a report title. For this, we will use
the static text Aircraft Report. To add the static text, we need to use the Static Text
component in the Palette.
We then need to drag the Static Text component to the Title area of the report.
iReport, by default, inserts the text Static text inside this field. To modify this default
text, we can double-click anywhere inside the field and type in a more appropriate
title. Alternatively, we can modify the Text property for the static text field in the
Properties window at the lower righthand side.
In the Properties window, we can modify other properties for our text. In the above
screenshot, we modified the text size to be 18 pixels, and we made it bold by clicking
on the checkbox next to the Bold property.
We can center the report title within the Title band by right-clicking on it, selecting
Position, and then Center.
After following all of these steps, our report should now look like this:
Applying the same techniques used for adding the report title, we can add some
more static text fields in the page header. After adding the page header, our report
now looks like this:
We modified the Vertical Alignment of all three text fields in the page header
by selecting the appropriate values in the Properties window for each one of them.
Now it is time to add some dynamic data to the report. We can enter a report
query selecting the report node in the Report Inspector window and then selecting
Edit Query.
As we type the report query, by default iReport retrieves report fields from it. This
query will retrieve the tail number, serial number, and model of every aircraft in the
database.
Now that we have a query and report fields, we can add text fields to the report. We
can do so by dragging the fields in the Report Inspector window to the appropriate
location in the report template.
After aligning each text field with the corresponding header, our report should now
look like this:
To avoid extra vertical space between records, we resized the Detail band by
dragging its bottom margin up. The same effect can be achieved by double-clicking
on the bottom margin.
Notice that we have an empty Column Header band in the report template. This
empty band will result in having some whitespace between each header and the first
row in the Detail band. To avoid having this whitespace in our report, we can easily
delete this band by right-clicking on it in the Report Inspector window and selecting
Delete Band.
We now have a simple but complete report. We can view it by clicking on Preview.
That’s it! We have created a simple report graphically with iReport.






October 1, 2009
Java