123
返回列表 发新帖
楼主: jieforest

Creating Dynamic Reports from Databases Using JasperReports 3.5

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
21#
 楼主| 发表于 2009-9-3 23:27 | 只看该作者
As 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 wished to save the report to a file, so we chose to use fillReportToFile(). This method fills the report with data from the datasource and saves it to a file in the filesystem. It has the potential of throwing a JRException if there is something wrong. Consequently, 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:

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
22#
 楼主| 发表于 2009-9-3 23:27 | 只看该作者
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
  {
    Connection connection;
    Statement statement;
    ResultSet resultSet;

    response.setContentType("application/pdf");
    ServletOutputStream servletOutputStream = response
                                                   .getOutputStream();
    InputStream reportStream = getServletConfig().getServletContext()
                   .getResourceAsStream("/reports/DbReportDS.jasper");

    try
    {
      String query = "select a.tail_num, a.aircraft_serial, "
                      + "am.model as aircraft_model, ae.model as
                         engine_model from aircraft a, "
                      + "aircraft_models am, aircraft_engines ae
                         where a.aircraft_engine_code in ("
                      + "select aircraft_engine_code from
                         aircraft_engines "
                      + "where horsepower >= 1000) and
                      am.aircraft_model_code = a.aircraft_model_code "
                      + "and ae.aircraft_engine_code =
                         a.aircraft_engine_code";

      Class.forName("com.mysql.jdbc.Driver");

      connection = DriverManager.getConnection("jdbc:mysql:
           //localhost:3306/flightstats?user=dbuser&password=secret");
      statement = connection.createStatement();
      resultSet = statement.executeQuery(query);

            JRResultSetDataSource resultSetDataSource = new
                                     JRResultSetDataSource(resultSet);

      JasperRunManager.runReportToPdfStream(reportStream,
             servletOutputStream, new HashMap(), resultSetDataSource);

            resultSet.close();
      statement.close();
      connection.close();


                  servletOutputStream.flush();
      servletOutputStream.close();
    }
    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());
    }
  }
}

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
23#
 楼主| 发表于 2009-9-3 23:27 | 只看该作者
This code is very similar to the previous examples. It executes an SQL query through JDBC and wraps the resulting resultSet 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 article 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).

A comparison of database report methods
Although embedding a database query into a report template is the simpler way that JasperReports allows us to create database reports, it is also the least flexible one. Using a JRResultSetDataSource
involves writing some more code but results in more flexible reports, as the same report template can be used for different datasources.
Which method to use depends on our needs. If we are sure that we will always be using a database as a datasource for our report, and 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 datasource provides the most flexibility.
Some report design tools will only generate database reports by embedding a database query into the report template. If we are using one of these tools, then we have little choice but to use this method. We are free to remove the <queryString> element from the JRXML after we are done designing the report and passing the JRResultSetDataSource at runtime. However, if we do this, we lose the ability to modify the report template from the report designer.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
24#
 楼主| 发表于 2009-9-3 23:27 | 只看该作者
Summary
In this article, we learned the different ways we can create database reports and how to use the <queryString>
JRXML element to embed SQL queries in a report template.
Additionally, we saw how to populate an instance of JRResultSetDataSource
with data from a result set and use it to fill a report. We also covered how to declare report fields to access data from individual columns in the result set of the query used to fill the report. Finally, we learned how to generate reports that are displayed both in the user's web browser and in PDF format.

使用道具 举报

回复
论坛徽章:
7
ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:442008新春纪念徽章
日期:2008-02-13 12:43:03祖国60周年纪念徽章
日期:2009-10-09 08:28:002010新春纪念徽章
日期:2010-03-01 11:19:072011新春纪念徽章
日期:2011-01-04 10:37:10复活蛋
日期:2011-06-15 16:36:29ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15
25#
发表于 2009-9-6 22:18 | 只看该作者
这种技术文章发到你的BLOG里面去吧!

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
26#
发表于 2009-9-6 22:46 | 只看该作者
好东西

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
27#
 楼主| 发表于 2009-9-7 09:16 | 只看该作者
原帖由 jrmd 于 2009-9-6 22:18 发表
这种技术文章发到你的BLOG里面去吧!


我的blog没人看,不如在这里大家共享。

使用道具 举报

回复
论坛徽章:
468
28#
发表于 2009-9-9 22:21 | 只看该作者
太多太长。。。

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
29#
发表于 2009-9-10 22:44 | 只看该作者
原帖由 jieforest 于 2009-9-7 09:16 发表


我的blog没人看,不如在这里大家共享。

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
30#
发表于 2009-9-10 22:44 | 只看该作者
原帖由 kso 于 2009-9-9 22:21 发表
太多太长。。。

你不是不懂英文吗

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表