1

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. Is it possible to pass multiple resultset using JRResultSetDataSource or any other option?

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);
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
Arun Raj
  • 243
  • 1
  • 7
  • 21

1 Answers1

1

You can send as many resultsets as you want in the parameters map.

Map reportParams = new HashMap(); reportParams.put("ds1", new JRBeanCollectionDataSource(beanCollection1)); reportParams.put("ds2", new JRBeanCollectionDataSource(beanCollection2));

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

Make sure to declare the parameters in the report with the same names (ds1, ds2), and set the ParameterClass as

net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

Now you can retrieve them with $P{ds1},$P{ds2} and so on. 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.

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). Now, under dataset1 (which, as I said, is a subdataset, so it allows parameters, fields, variables), I declare the fields:

<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.

Laura
  • 1,552
  • 12
  • 12
  • You can see my response on [link](http://stackoverflow.com/questions/22045124/two-datasource-in-report/22146977#22146977) for examples. – Laura Mar 03 '14 at 12:42
  • How can i cast ResultSet into Collection and pass into JRBeanCollectionDataSource – Arun Raj Mar 04 '14 at 06:52
  • For example if beanCollection1 contains empno,empname,empdepartment means how can i map with Jasper Reports using key value ds1 parameter.How to specify these fields in dataset. – Arun Raj Mar 04 '14 at 08:06
  • Right, I'm sorry, my bad. No need to cast, you should be able to do exactly the same with JRResultSetDataSource instead of JRBeanCollectionDataSource. – Laura Mar 04 '14 at 08:19
  • Add your fields just as you would usually do, but in the subDataset. Then in the element that uses the subDataset (ex. the table) you can access them with $F{empnp},$F{empname},$F{empdepartment}. – Laura Mar 04 '14 at 08:33
  • I add resultset method to JRResultSetDataSource and put into hashmap like this hashmap.put("list1",new JRResultSetDataSource(execute())). Method executes and values in resultset.But in Jasper Reports displays nothing.I had one doubt,in this map list1 contains resultset values of execute() method but in jasper without using this list1 key how to retrieve values for dataset. – Arun Raj Mar 04 '14 at 10:02
  • Let me get this straight. What are you doing with the list in the report? Is it a table as I supposed? – Laura Mar 04 '14 at 11:21
  • I using list component and Database JDBC Connection.In my resultset having empno,empdepartment how to display this list in my Jasper Report.When i preview report displays the list values correctly.But when i generated pdf using JRResultSetDataSource list value get empty. – Arun Raj Mar 04 '14 at 11:33
  • In Connection/Datasource Expression->Use connection expression->$p{list}.Then i have created parameter list1 and parameter class as net.sf.jasperreports.engine.JRResultSetDataSource.But when i compile show error like this java.lang.NoSuchMethodException:  net.sf.jasperreports.engine.JRResultSetDataSource – Arun Raj Mar 04 '14 at 13:02
  • That's odd. I can't see any reason why it would search it as a method. Can you post your jrxml? – Laura Mar 04 '14 at 13:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48957/discussion-between-arun-raj-and-laura) – Arun Raj Mar 04 '14 at 13:23