Building Your First iReport

Exclusive offer: get 50% off this eBook here
iReport 3.7

iReport 3.7 — Save 50%

Learn how to use iReport to create, design, format, and export reports

$23.99    $12.00
by Shamsuddin Ahammad | March 2010 | Java Open Source

We will develop our first simple report using iReport, and before doing so, we will create a connection/data source that will be used to fill the reports.

In this article by Shamsuddin Ahammad, we will learn about:

  • Creating a new data source or database JDBC connection
  • Creating a report using the wizard
  • Using the design query feature to produce the SQL query for the report
  • Viewing and exporting the report using the built-in viewer

So let's get on with it!

Creating a connection/data source

Before going to create the connection, a database should be set up. The SQL query for the database used for creating reports can be downloaded from the Packt website. Now, we are going to create a connection/data source in iReport and build our first report in some easy to follow steps:

You need to create the connection/data source just once before developing the first report. This connection will be reused for the following reports.

  1. Start iReport.
  2. Press the Report Datasources button in the toolbar. You will see a dialog box similar to the following screenshot:
  3. iReport 3.7

  4. Press the New button. Another dialog box will appear for selecting the data source type. There are several types to choose from, according to your requirement. For now, choose Database JDBC connection, and press Next >.
  5. iReport 3.7

  6. Another dialog box will appear to set up the Database JDBC connection properties. Give a sensible name to the connection. In this case, it is inventory.
  7. Choose the JDBC Driver from the list, according to your connection type and/or your database. In this case, it is MySQL (com.mysql.jdbc.Driver).
  8. Write the JDBC URL, according to the driver you have chosen. For this tutorial, it is jdbc:mysql://localhost/inventory.
  9. In the previous code for connecting to a database from a Java program using JDBC—jdbc is the connection protocol, mysql is the subprotocol, localhost is the MySQL server if it runs on the same computer, and inventory is the database name.

  10. Enter the Username and Password. Generally, for a MySQL server, the username is root and you have set a customized password during the installation of the MySQL server. The screenshot is as follows:
  11. iReport 3.7

  12. Press Test to confirm that you have set all the properties correctly. If all the settings are correct, then you will see a message that says Connection test successful!.
  13. You can save the password by checking the Save Password checkbox, but be warned that iReport stores passwords in clear text. Storing passwords in clear text is a bad thing for us, isn't it? If you do not specify a password now, iReport will ask you for one only when required and will not save it.

  14. Now save the connection. You will see that the newly created connection is listed in the Connections/Datasources window. If you have more than one connections, then you can set one as the default connection. In order to do this, select the connection and press Set as Default.
  15. Enter the Username and Password. Generally, for a MySQL server, the username is root and you have set a customized password during the installation of the MySQL server. The screenshot is as follows:

iReport 3.7

When we execute the report with an active connection, the reports are filled with data from the database or other data sources. We can also see the report output with empty data sources, which has, by default, a single record with all fields set to null. An empty data source is used to print a static report. However, in order to choose the tables and columns from a database automatically using the Report Wizard, we need to connect to a database/data source first. To do this, we must create a connection/data source.

Building your first report

Having set up a connection, we are ready to build our first report. We will keep it very simple, just to be familiar with the steps required for building a report. We will create a report that lists out all the products; that is, we will show all the rows of the product table of our database.

Follow the steps listed and build your first report:

  1. Go to the File menu and click New…. You will see a dialog box like the following screenshot:
  2. iReport 3.7

  3. From the list of Report templates, select Simple Blue and press Launch Report Wizard.
  4. iReport 3.7

  5. Enter Report name as List of Products and press Next >.
  6. Now you will specify the query to retrieve the report fields. Select your connection from the Connections / Data Sources drop-down list.
  7. iReport 3.7

  8. Write the SQL query for the report you want to develop. In our case, it is
      SELECT ProductCode, Name, Description FROM Product.
iReport 3.7 Learn how to use iReport to create, design, format, and export reports
Published: April 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:
  1. Alternatively, you can use the Design query option if you want to design the query graphically by selecting tables and columns easily, without writing the SQL commands. For this, double-click on the table name, select the fields from the table, and then press OK.
  2. iReport 3.7

  3. After writing the SQL query or designing the query, press Next >.
  4. iReport 3.7

  5. In this step, we will select the fields that we want to show in the report. We may select all the fields or choose some of them. For this example, select all the fields (ProductCode, Name, and Description) by clicking on >> (double arrow). Note that the fields listed here are based on the query specified in the previous step.
  6. iReport 3.7

  7. After selecting the fields, press Next >.
  8. Now we are in step 5 (Group by…) of the wizard. Ignore this just for now; we will learn about grouping later. Press Next > to proceed.
  9. We are at the last step of the Report Wizard. Now press Finish.
  10. iReport 3.7

  11. You will see the following output:
  12. iReport 3.7
  13. To change the title, select TITLE, and enter List of Products in the Text box of the Properties window, at the bottom-right corner. You can change the font name, size, and style according to your requirements from the same window. In this example, Font name is SansSerif, Size is 20, and style is Bold.
  14. iReport 3.7
  15. It's time to see the output of your first report. Just press the Preview button at the top of the report design. You will see a report similar to the following one:

iReport 3.7

To build the report easily, we can use the Report Wizard, where the database columns can be selected from the list, and the layout of the report can be set very easily by choosing templates. Note that for choosing the layout, the tabular format is generally suitable if your report shows more than one row from the database table, and the column format is suitable if the query returns only a single row. If the number of columns in the query result is quite large, then you can choose the landscape template.

Viewing and exporting the report

We can view our report using JRViewer or any other supported external programs, such as Acrobat Reader (PDF preview), Microsoft Word (RTF preview), any browser (HTML preview), and so on. You have seen the JRViewer preview. There is a built-in toolbar in the JRViewer; here you can save the report in various formats, such as PDF, RTF, HTML, CSV, XML, and so on. To do this, just press the Save button, and then choose PDF/ RTF/ HTML/ CSV/ XML or others from the Files of type drop-down list.

You can print the report from the viewer by just pressing the Print button in the toolbar. If your report contains more than one page, then you can navigate through the report pages using these four buttons: First Page, Previous Page, Next Page, and Last Page from the toolbar. You can resize the page to Fit the page, Fit the page width, or Actual size. You can also zoom in or zoom out. All options are available in the toolbar—you just have to choose the appropriate button.

You can change the report viewer from the Preview menu to see the preview in another application. You can choose from PDF, HTML Java 2D, RTF, or other preview options. However, before seeing these previews, you have to set the external programs for the appropriate viewers. You can do this from Tools | Options | iReport | Viewers. Browse the required programs for each viewer. Then choose the appropriate viewer from the Preview menu, and preview the report again.

iReport 3.7

Summary

In this article, we had a look at creating connections and building simple reports.

Specifically, we covered:

  • Creating a database JDBC connection
  • Using the Report Wizard
  • Using the design query feature for designing a query without writing the SQL command
  • Choosing the appropriate layout and template
  • Viewing report in JRViewer and other external programs
iReport 3.7 Learn how to use iReport to create, design, format, and export reports
Published: April 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Shamsuddin Ahammad

Shamsuddin Ahammad is a Senior Lecturer and the Course Coordinator at Daffodil Institute of IT, Bangladesh. He has been teaching Java, Programming Methods, and Database Systems since 2002. He has experience in supervising hundreds of academic projects. Shamsuddin has a Masters degree in Management Information Systems (MIS) from Daffodil International University, Dhaka. He obtained the BSc(Hons) degree in Computing & Information Systems (CIS) of NCC Education Ltd, UK and London Metropolitan University joint programme from Daffodil Institute of IT. Before that, he completed the IDCS & IADCS of NCC Education Ltd. He is an Additional Reviewer of Conference on Quality Engineering in Software Technology (CONQUEST) organized by International Software Quality Institute (iSQI) in Germany. He is the author of book titled iReport 3.7 published from PACKT Publishing in March 2010.

Books From Packt


Joomla! 1.5 Multimedia
Joomla! 1.5 Multimedia

Drupal 6 Performance Tips
Drupal 6 Performance Tips

MooTools 1.2 Beginner's Guide
MooTools 1.2 Beginner's Guide

Tomcat 6 Developer's Guide
Tomcat 6 Developer's Guide

Apache Roller 4.0 – Beginner's Guide
Apache Roller 4.0 – Beginner's Guide

Grok 1.0 Web Development
Grok 1.0 Web Development

jQuery 1.4 Reference Guide
jQuery 1.4 Reference Guide

GlassFish Administration
GlassFish Administration


Your rating: None Average: 4.5 (2 votes)
... by
thank you!!!
hii by
This is Excellent
Royrambo by
Bravo.. thanks for the articles

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
q
r
u
p
r
q
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software