Java Reference
In-Depth Information
As can be seen in this example, to provide a report with database data, by using
JRResultSetDataSource , we must execute the database query from the Java code,
and wrap the resulting resultSet object into an instance of JRResultSetDataSource ,
by passing it to its constructor. The instance of JRResultSetDataSource must
then be passed to the JasperFillManager.fillReportToFile() method. Strictly
speaking, any method that takes an instance of a class implementing JRDataSource
can be called. In this example, we wish to save the report to a file. Therefore, we
chose to use the fillReportToFile() method. This method fills the report with
data from the datasource and saves it to a file in the file system. It has the potential of
throwing a JRException , if there is something wrong. Therefore, this exception must
either be caught, or declared in the throws clause.
After executing this code, a report, identical to the first one we saw in the previous
section, is generated. The following example demonstrates how a web-based report
can be created by using a database datasource:
package net.ensode.jasperbook;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperRunManager;
public class DbDSReportServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
 
Search WWH ::




Custom Search