4 min read

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here