To start iReport for the first time, simply execute the provided startup script. iReport provides two such scripts: startup.bat for Windows and startup.sh for Unix-like OSes, both of which are located in the <ireport_dir>/bin/ directory. When iReport starts, it will display directory information in the output pane. You will see something similar to:
iReport home (ireport.home system property):.
User home ( user.home system property): C:\WINNT\Profiles\yourusername\.ireport.
iReport default compile directory: D:\temp
Preparing the Database
Of course, our report is useless without a source of data with which to populate it. For this, we'll be using a MySQL database accessed via JDBC.
The first step, of course, is creating the database. Since this is just an example app, we'll also insert the data during this step. Click here to download a text file containing the commands used to create and populate the database. Once the DB is up and populated, it's time to set up a way for our report to access it.
Since this webapp is going to be served by Tomcat, we should take advantage of Tomcat's connection management. To do so, we'll define a DataSource as a JNDI resource in the application's context. The document fragment below is the context.xml the sample app uses.
01 <Context path="/jasper_example" docBase="jasper_example" debug="5" reloadable="true"
02 crossContext="true">
03 <Resource name="jdbc/jasper_example" auth="Container" type="javax.sql.DataSource" />
04 <ResourceParams name="jdbc/jasper_example">
05 <parameter>
06 <name>factory</name>
07 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
08 </parameter>
09 <!-- MySQL Server DB username and password for DB connections -->
10 <parameter>
11 <name>username</name>
12 <value>username</value>
13 </parameter>
14 <parameter>
15 <name>password</name>
16 <value>password</value>
17 </parameter>
18 <!-- Class name for the JDBC driver -->
19 <parameter>
20 <name>driverClassName</name>
21 <value>com.mysql.jdbc.Driver</value>
22 </parameter>
23 <!-- The JDBC connection url for connecting to your MySQL Server DB. -->
24 <parameter>
25 <name>url</name>
26 <value>jdbc:mysql://127.0.0.1/jasperarticle</value>
27 </parameter>
28 <!-- maximum active connections -->
29 <parameter>
30 <name>maxActive</name>
31 <value>3</value>
32 </parameter>
33 <!-- maximum idle connections -->
34 <parameter>
35 <name>maxIdle</name>
36 <value>3</value>
37 </parameter>
38 <!-- connections attempts timeout after 10 seconds -->
39 <parameter>
40 <name>maxWait</name>
41 <value>10000</value>
42 </parameter>
43 </ResourceParams>
44 </Context>
Creating a New Report
Create a Database Connection
To create a new data source to use in your report select "Datasource" from the iReport menu bar, then "Connections / Datasources". Click the "New" button and select "Database JDBC connection" from the first dropdown list. If connecting to a MySQL database, select the com.mysql.jdbc.Driver JDBC Driver. After completing the connection parameters, you should have a JDBC URL similar in form to: jdbc:mysql://192.168.1.100/NameOfYourDB.
If when you test your connection you receive an error dialog with the following message, "Communication link failure: java.io.IOException, underlying cause: Unexpected end of input stream", you should update your MySQL JDBC driver. The latest driver can be found at:
http://www.mysql.com/products/connector/j/.The MySQL driver that ships with iReport does not seem compatible with version 4.1 of MySQL.
At the time of this writing, mysql-connector-java-3.1.8a.zip was the latest production-ready archive available for download. Inside this Zip file, you will find a file named mysql-connector-java-3.1.8-bin.jar; copy this file into the iReport/lib directory, e.g., C:\Program Files\iReport-0.4.1\lib\, and delete any older versions, such as mysql-connector-java-3.0.8-stable-bin.jar.
When iReport starts, it will automatically load all .jar files found in the \lib directory. Restart iReports and test your new Datasource connection.
Creating a New Report File
To create a new report select "File / New Document" from the menu bar. Name the report and select your desired paper size. Do not use spaces in the report name. Next, you will need to set the active database connection in order to use values from your database queries. To do this, select "Build / Set active connection" from the menu and choose the connection defined earlier.
Add Fields From Database Table
First, define a query. Select "Datasource / Report query" from the menu bar; this will open the query dialog where we will create a select query to return values to our report. Enter the following: SELECT ID, Name, Address, City, PostalCode FROM Customer. Note: Explicitly select columns, otherwise only non-null columns will appear in the Report Fields selection dialog.As you type this query, iReport will attempt to execute the query as you type.