Finally, we need to write some code to retrieve the data from the database and generate the report. The following example demonstrates one way to accomplish this:
package net.ensode.jasperrdb;
//Non JasperReports related imports omitted
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
public class JasperDbDemo
{
public static void main(String[] args) throws SQLException
{
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try
{
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost/mydb?user=user&password=secret";
String query = " select c.course_cd, c.course_nm, i.first_nm, "
+ "i.last_nm from instructors i, courses c where "
+ "i.instructor_id = c.instructor_id order by course_nm";
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
File reportFile = new File("reports/Database_Report.jasper");
JasperRunManager.runReportToPdfFile(reportFile.getPath(), new HashMap(),
new JRResultSetDataSource(resultSet));
}
catch (Exception e)
{
//handle the exception
}
finally
{
//boilerplate code to close the result set, statement, and exception
}
}
}
Relevant sections of the code are shown in bold. This example assumes the JRXML file has already been compiled into a Jasper file. Refer to Getting Started With JasperReports if you need a refresher on how to accomplish this.
The runReportToPdfFile()
method of the JasperRunManager
class generates a report and immediately saves it as a PDF file, using
the report name defined in the JRXML file as the file name. Its first
parameter is a String
containing the absolute path of the
compiled report to be generated. Its second parameter is an instance of
a class implementing the java.util.Map
interface, it is
used to pass any parameters to the report. In our example, we are not
passing any parameters to the report, therefore we pass an empty HashMap
as the second parameter to this method. The third parameter is an
instance of a class implementing the net.sf.jasperreports.engine.JRDataSource
interface. In our example, we pass an instance of net.sf.jasperreports.engine.JRResultSetDataSource
.
The constructor of the JRResultSetDataSource
takes the ResultSet
containing the data we want to pass to the report as a parameter. After
calling the runReportToPdfFile()
method of the JasperRunManager
class, the report is generated, exported to a PDF and saved to disk. A
similar technique can be used for web applications, but redirecting the
PDF report to the browser instead of saving it to disk. See Displaying JasperReports
PDF Reports on the Browser for details.
This article demonstrated how to generate reports dynamically from
database data using JasperReports. By using the JRResultSetDataSource
utility class, a ResultSet
is passed to the report, the
data in the ResultSet
is read from the compiled report,
which is generated from the JRXML file.