简体   繁体   English

如何使用JasperReports为单个报表传递多个结果集?

[英]How to pass several resultsets for single report using JasperReports?

In my report I have a set of fields and two datasets. 在我的报告中,我有一组字段和两个数据集。 I want to execute three procedure at a time to run a report. 我想一次执行三个过程来运行报告。 First procedure for execute set of fields and remaining two procedure for datasets. 执行字段集的第一个过程,数据集的剩余两个过程。 I pass one resultset to execute a report is working fine. 我通过一个结果集来执行报告,效果很好。 But I want to pass two more resultset from execute() and port() methods. 但我想再传递两个来自execute()和port()方法的结果集。 Is it possible to pass multiple resultset using JRResultSetDataSource or any other option? 是否可以使用JRResultSetDataSource或任何其他选项传递多个结果集?

public ResultSet execute() throws ClassNotFoundException {
    Statement stmt;
    ResultSet resultset = null;
    Connection con = null;
    try {
        String selectstatement = "CALL P_Select_Salary2 ('2013-01-01', '2013-01-31', 1, 'Salary_OA')";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = con.createStatement();
        resultset = stmt.executeQuery(selectstatement);

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        close(con);
        close(resultset);
    }
    return resultset;
}

public ResultSet port() throws ClassNotFoundException {
    Statement stmt;
    ResultSet resultset = null;
    Connection con = null;
    try {
        String selectstatement = "CALL P_Select_Salary3 ('2013-01-01', '2013-01-31', 1, 'Salary')";
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = con.createStatement();
        resultset = stmt.executeQuery(selectstatement);

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        close(con);
        close(resultset);
    }
    return resultset;
}

public void generateReport() {
    Connection connection = null;
    Statement stmt;
    ResultSet resultset = null;
    try {
        String selectstatement = "CALL P_Select_Salary ('2013-01-01', '2013-02-28', 1, 'Salary_Summary')";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/compliance?user=root&password=root");
        stmt = connection.createStatement();
        resultset = stmt.executeQuery(selectstatement);
        JRResultSetDataSource resultsetdatasource = new JRResultSetDataSource(resultset);
        String realpath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("common/reports/wageslip.jasper");
        jasperprint = JasperFillManager.fillReport(realpath, new HashMap(), resultsetdatasource);
        HttpServletResponse httpservlet = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpservlet.addHeader("Content-disposition", "attachment;filename=wageslip.pdf");
        ServletOutputStream servletout = httpservlet.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperprint, servletout);
        FacesContext.getCurrentInstance().responseComplete();
    } catch (net.sf.jasperreports.engine.JRException JRexception) {
        logger.info("JRException Exception" + JRexception.getMessage());
        JsfUtil.addErrorMessage("No Datas between FromDate and ToDate");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(connection);
        close(resultset);
    }
}

You can send as many resultsets as you want in the parameters map. 您可以在参数映射中发送任意数量的结果集。

Map reportParams = new HashMap(); 地图reportParams = new HashMap(); reportParams.put("ds1", new JRBeanCollectionDataSource(beanCollection1)); reportParams.put(“ ds1”,新的JRBeanCollectionDataSource(beanCollection1)); reportParams.put("ds2", new JRBeanCollectionDataSource(beanCollection2)); reportParams.put(“ ds2”,新的JRBeanCollectionDataSource(beanCollection2));

JasperPrint jrprint = JasperFillManager.fillReport(jasperReport,reportParams, new JREmptyDataSource()); JasperPrint jrprint = JasperFillManager.fillReport(jasperReport,reportParams,新的JREmptyDataSource());

Make sure to declare the parameters in the report with the same names (ds1, ds2), and set the ParameterClass as 确保在报表中声明具有相同名称(ds1,ds2)的参数,并将ParameterClass设置为

net.sf.jasperreports.engine.data.JRBeanCollectionDataSource net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

Now you can retrieve them with $P{ds1},$P{ds2} and so on. 现在,您可以使用$ P {ds1},$ P {ds2}等检索它们。 You haven't specified what you need them for, but you can do practically anything with the parameters, like set one of them of the datasource of a table etc. 您尚未指定所需的内容,但实际上可以使用参数进行任何操作,例如设置表的数据源之一。

Edited after comments : 评论后编辑

I have a list component, to which I set Connection/Datasource Expression=$P{list1} , where $P{list1} is a parameter of type net.sf.jasperreports.engine.JRResultSetDataSource . 我有一个列表组件,向其设置了Connection/Datasource Expression=$P{list1} ,其中$ P {list1}是net.sf.jasperreports.engine.JRResultSetDataSource类型的参数。

My list component will look like this: 我的列表组件将如下所示:

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="dataset1">
                        <dataSourceExpression><![CDATA[$P{ds1}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="66" width="400">
                        <textField>
                            <reportElement x="10" y="10" width="100" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{empname}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>

As you can see, I have the element Dataset1 is the dataset added automatically in the report when you added the list component (if you use iReport for the design). 如您所见,我有元素Dataset1是添加列表组件(如果您使用iReport进行设计)时在报表中自动添加的数据集。 Now, under dataset1 (which, as I said, is a subdataset, so it allows parameters, fields, variables), I declare the fields: 现在,在dataset1(正如我所说的,它是一个子数据集,因此它允许参数,字段,变量)下,我声明这些字段:

<subDataset name="dataset1">
        <field name="empno" class="java.lang.Integer"/>
        <field name="empname" class="java.lang.String"/>
    </subDataset>

That's it. 而已。 I have tried this exact code, works for me. 我已经试过这个确切的代码,对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM