问题
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);
}
}
回答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.
来源:https://stackoverflow.com/questions/22113285/how-to-pass-several-resultsets-for-single-report-using-jasperreports