Creating Reports with iReports and JasperReports
June 23, 2005
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.
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>

