View
215
Download
0
Embed Size (px)
Citation preview
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 1/9
Designing a report
In this tutorial, we cover the basics of how to design a report. The report
creation process has three main steps:
Creating a data source or a database connection used to ll the report.
Designing the report, including the layout of its elements and parameters to
represent the data.
Runing the report, which includes compiling the R!"# source le in a asper
le and lling in the data for e$port or onscreen display.
To %eep things simple, this tutorial uses a plain D&C connection, which is a
connection to a database. The data to print is retrieved using an '(# )uery
embedded in the report.
#et*s start by creating the connection. +ou can use any database and table
you want, but if you want to test the same e$act )uery and create the same
report as the tutorial, you can use the sample database included with
asperReports. nce you download asperReports -from here and unpac% the
archive, go to the demo/hs)ldb directory and run the following command:
0ava 1cp ../../lib/hs)ldb12.3.4124.0ar org.hs)ldb.'erver
5hen the 6'(#D& server starts up correctly, the terminal output appears as
follows:
7igure 2
To create the new connection in iReport, open the datasources/connectionsdialog by clic%ing its icon in the tool bar, as shown in 7igure 8.
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 2/9
Clic% 9ew, choose Database D&C connection as shown in 7igure , and usethe following settings for the sample 6'(# database, as shown in 7igure ;below:
9ame: asperReports 'ample
D&CDriver: org.hs)ldb.0dbcDriver
<rl: 0dbc:hs)ldb:hs)l://localhost
<sername: sa
=assword: >none?
7igure
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 3/9
7igure ;
@s said above, you can use your favorite database, in that case you have toprovide the right <R#, the D&C driver and credentials. iReport proposes a listof well %nown database drivers. If the one you need is not present, 0ust typethe name. If it is present and it is highlighted in red it means that the driver isnot available in the classpath and must be added. To do so, select Tools Aptions A iReport A Classpath and add the re)uired @Rs to the classpath.
@lso, it*s good practice to test the connection before saving it. 5hen done,the new connection appears in the connections list and is automaticallyselected as the default, active connection:
7igure B
9ow, let*s create the new report. 'elect 7ile A 9ew.
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 4/9
7igure
In the template chooser that appears, select a blan% report and clic% penthis Template. 'elect a name and location to story the new le, clic% ne$t andthen nish. +our blan% report now appears in the designer:
7igure
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 5/9
Clic% the button representing a database )uery in the designer tool bar:
7igure 3
This opens the (uery dialog to dene the )uery that retrieves data to ll thereport. The '(# )uery is stored inside the report and can accept parametersto lter the data using the synta$ E=FG -for standard parameters and E=HFG-to use a parameter as '(# chun%. @gain we want to %eep things simple inthis tutorial, so we will not use any parameters. Type the following )uery toselect all the elds/columns in the orders table:
select from orders
7igure J
f course, you can use any table for this e$ample. The orders table is
available in the sample database we are using. If you don*t %now '(#, youcan use the visual '(# )uery builder by clic%ing the (uery designer button.@s you type the )uery, iReport e$ecutes it to e$tract all the elds -columnsavailable in the )uery. The elds are listed on the bottom portion of thewindow. If there is an error in the '(#, you*ll get a message that e$plainswhat*s wrong. 5hen the )uery is valid, clic% K. iReport lists all thediscovered elds in the report inspectory, so they can be used in the reportand displayed using te$t elds and other elements. To print the value of a
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 6/9
eld in the report, drag a eld ob0ect from the report inspector to the designview. 7or e$ample, drag a eld into the detail band, a section of the reportthat is printed for each record/row in the )uery results. The title and thesummary bands are printed 0ust once in each report, while the page headerand footer are printed once at the top and bottom of each page, respectively.
7igure 24
5hen the eld ob0ect is dragged inside the detail band, iReport creates ate$teld element and sets the te$teld e$pression for that element toE7F'6I=9@"LG. This is a simple e$pression to print the value of the eld'6I=9@"L -the synta$ E7FG. Repeat the same operation for other elds andformat the te$telds element by stretching, dragging and aligning them. It isimportant that the te$telds are all placed within the detail band. To addother elements -such lines or labels, drag them from the palette shown in7igure 22 into the designer view, then resiMe and arrange them as desired.
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 7/9
7igure 22In this case, we add a label in the title band for the report title, we addcolumn labels using label elements placed in the column header band, andwe place a thin line 0ust under the te$telds in the detail band. 7inally, weresiMe some bands and remove others by setting a their height to 4. Changethe height of band by dragging its bottom edge. @ shortcut to reduce theheight of the band is to double1clic% on its bottom edge, this will set it to thebottom edge of its lowest element.
+ou 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 theproperties of the currently selected element in the designer. Clic% on anelement in the designer or in the report inspector to select it and view itsproperties. 7or bands, you can also clic% in an unused part of the band. 7igure28 shows the nal design of the sample report.
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 8/9
7igure 28
To preview the report, switch to the preview mode by clic%ing =review in thedesigner tool bar. The preview compiles the report in the bac%ground and llsit with data retrieved by the )uery through the D&C connection. @s shown in7igure 2, the detail band repeats for every row in the )uery results, creatinga simple table report.
8/10/2019 Designing a Report_Jasper
http://slidepdf.com/reader/full/designing-a-reportjasper 9/9
7igure
Congratulations, you have 0ust created your rst reportH 9ow you*re ready toe$port a report.