![]() |
|
![]() |
||||||||||||
![]() |
||||||||||||||
|
Designing a report by Giulio Toffoli
In this tutorial, we cover the basics of how to design a report. The report creation process has three main steps: java -cp ../../lib/hsqldb-1.8.0-10.jar org.hsqldb.Server
When the HSQLDB server starts up correctly, the terminal output appears as follows: ![]() Figure 1 To create the new connection in iReport, open the datasources/connections dialog by clicking its icon in the tool bar, as shown in Figure 2. ![]() Figure 2 Click New, choose Database JDBC connection as shown in Figure 3, and use the following settings for the sample HSQL database, as shown in Figure 4 below:
As said above, you can use your favorite database, in that case you have to provide the right URL, the JDBC driver and credentials. iReport proposes
a list of well known database drivers. If the one you need is not present, just type the name. If it is present and it is highlighted in red it means
that the driver is not available in the classpath and must be added. To do so, select Tools > Options > iReport > Classpath and add the
required JARs to the classpath. Also, it's good practice to test the connection before saving it. ![]() Figure 5 Now, let's create the new report. Select File > New. ![]() Figure 6 In the template chooser that appears, select a blank report and click Open this Template. Select a name and location to story the new file, click next and then finish. Your blank report now appears in the designer: ![]() Figure 7 Click the button representing a database query in the designer tool bar: ![]() Figure 8 This opens the Query dialog to define the query that retrieves data to fill the report. The SQL query is stored inside the report and can accept parameters to filter the data using the syntax $P{} (for standard parameters) and $P!{} (to use a parameter as SQL chunk). Again we want to keep things simple in this tutorial, so we will not use any parameters. Type the following query to select all the fields/columns in the orders table: select * from orders ![]() Figure 9 Of course, you can use any table for this example. The orders table is available in the sample database we are using.
If you don't know SQL, you can use the visual SQL query builder by clicking the Query designer button. ![]() Figure 10 When the field object is dragged inside the detail band, iReport creates a textfield element and sets the textfield
expression for that element to $F{SHIPNAME}. This is a simple expression to print the value of the field SHIPNAME (the syntax $F{}).
Repeat the same operation for other fields and format the textfields element by stretching, dragging and aligning them. It is important that the textfields
are all placed within the detail band. ![]() Figure 11 In this case, we add a label in the title band for the report title, we add column labels using label elements placed
in the column header band, and we place a thin line just under the textfields in the detail band. Finally, we resize some bands and remove others by setting
a their height to 0. Change the height of band by dragging its bottom edge. A shortcut to reduce the height of the band is to
double-click on its bottom edge, this will set it to the bottom edge of its lowest element. You can also set properties such as the height of the band in the property sheet under the palette. The property
sheet lets you view and edit the properties of the currently selected element in the designer. Click on an element in the designer or in the report
inspector to select it and view its properties. For bands, you can also click in an unused part of the band. ![]() Figure 12 To preview the report, switch to the preview mode by clicking Preview in the designer tool bar. The preview compiles the report in the background and fills it with data retrieved by the query through the JDBC connection. As shown in Figure 13, the detail band repeats for every row in the query results, creating a simple table report. ![]() Figure 13 Congratulations, you have just created your first report! Now you're ready to export a report. |
||||||||||||||
![]() |
|
![]() |