In this tutorial we will see how to connect to a database, execute a query, retrive fields information and grouping
rows with a group band.
For "detailed" information about how to configuring iReport for enabling database connections (you must simply setup the classpath to find
JDBC driver classes), see the tutorial "Tutorial 1: How to compile and preview reports from iReport".
|
Step1: create a connection
1) Select menu Database->connections
2) Press "new" button to create a new connection
3) Fill for and test your new connection
nbsp;nbsp; In this tutorial we will use MySQL as DB, but the logic is the same for any database
4) Press "Save". Your new connection will appare in the connections list
5) Close the connections window
|
|
Step2: read query fields
1) Create a new report named "tutorial2"
2) Add a static text element to the title band
Pay ever attention to the band linked to your elements !!!
3) Save in a file named tutorial2.xml
4) Select menu Database->Report query
5) From connections combobox select myconnection
6) Write a selection query. In this tutorial we will select all records of table COMUNI (cities)
SELECT * FROM COMUNI ORDER BY PROVINCIA, COMUNE
7) Press button labeled "read fields", a new window will appare. Pleas wait... at the end the fields selected by the query
will be inserted in the fields listview
8) Press button "Register fields to report" for save fields definition
9) Press button "Save to report" for attach query to report
|
|
Step3: create a group
1) Open variables window. You would have to see the list of the fields
2) Select menu view->groups
3) Create a new group (pressing the "new" button) named "Group1"
4) Check the box "Start on a new column"
5) Insert the group expression. In this tutorial we want see all cities grouped by province
The expression value is: $F{PROVINCIA}
6) Press "OK" to close group definition window, and "close" to close groups list window
7) You will see the new group drawed on your document
|
|
Step4: drag and drop...
1) Reopen variables window. You would have to see the list of the fields
2) Drag the field selected for grouping (in my case PROVICIA) on the Group1 band...and drop. A new Field Element will be created
3) Drag another field on detail band
4) I have added at this point two lines, one linked with Group1Header band, and one linked with detail band.
5) Open the ReportProperties (view->report properties) and set num columns=2, columns width=250, spacing=35
|
|
Step5: Compilation and preview...
1) Open the compilator (edit->compile...)
2) Select your connection (for me is "myconnection")
3) Press "compile" button
|
|
Return to docs
|