Java Reference
In-Depth Information
}
catch (Exception e)
{
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
This code is very similar to the previous examples. It executes an SQL query via
JDBC and wraps the resulting result set in an instance of JRResultSetDataSource .
This instance of JRResultSetDataSource is then passed to the JasperRunManager.
runReportToPdfStream() method to export the report to PDF format and stream it
to the browser window.
All the examples in this chapter use simple SQL select queries to obtain report
data. It is also possible to obtain report data from the database by calling stored
procedures or functions (if supported by the RDBMS and JDBC driver we are using).
Database Report Methods Compared
Although embedding a database query into a report template is a simple way
to create database reports with JasperReports, it is also the least flexible. Using a
JRResultSetDataSource involves writing some more code, but results in more
flexible reports, since the same report template can be used for different datasources.
Depending on our needs, we choose the appropriate method. If we are sure we will
always be using a database as a datasource for our report, and that the database
query is unlikely to change much, then embedding the database query into the
JRXML template at design time is the most straightforward solution. If the query
is likely to change, or if we need to use datasources other than a database for our
reports, then using a JRResultSetDataSource provides the required flexibility.
 
Search WWH ::




Custom Search