Java Reference
In-Depth Information
connection.close();
}
catch (JRException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new DbReportFill().generateReport();
}
}
As can be seen in this example, a database connection is passed to the report in
the form of a
java.sql.Connection
object as the last parameter of the static
JasperFillManager.fillReportToFile()
method. The first two parameters are of
type
String
, used to indicate the location of the binary report template, or Jasper file,
and an instance of a class implementing the
java.util.Map
interface, used to pass
additional parameters to the report. Since for this report we don't need to pass any
additional parameters, we used an empty
HashMap
.
There are six overloaded versions of the
JasperFillManager.fillReportToFile()
method. Three of them take a
Connection
object as a parameter. Refer to Chapter 3
for a description of the other versions of this method that take a
Connection
object
as a parameter.
For simplicity, our examples open and close database connections every
time they are executed. It is usually a better idea to use a connection
pool, since connection pools increase performance considerably. Most
Java EE application servers come with connection pooling functionality.
The
Commons-dbcp
component of Jakarta Commons includes utility
classes for adding connection pooling capabilities to applications that do
not make use of an application server.