mfidan
mfidan

Reputation: 647

Can Jasper report use both connection and bean togehter?

In my case i need to use both JRBeanCollectionDataSource and embedded query in jasper report.

if i need a connection i feed the report as below ,

JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(is, hm, ds.getConnection() );

and if i have a collection to show in report, feed as below ,

JasperPrint jprint = (JasperPrint) JasperFillManager.fillReport(is, hm, getBeanDataSource());

getBeanDataSource method returns a JRBeanColleactionDataSource object contains the desired data in it .

Both works fine. But in some cases i need to feed a section of a report with beans and an other with query. Is there a way to do this ?

Upvotes: 3

Views: 283

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

Yes and no, The main report needs one datasource, but you can use sub reports within your report and if you like to send the datasource you do it trough the parameters.

HashMap<String,Object> hm= new HashMap<String,Object>();
hm.put("datasource", getBeanDataSource());

in jrxml

<parameter name="datasource" 
 class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>

the subreport to use this datasource would be something like this

<subreport>
  <reportElement positionType="Float" x="-20" y="28" width="595" height="102" isRemoveLineWhenBlank="true" uuid="84c78233-8870-4563-9138-e166bb6000fd"/>
   <dataSourceExpression><![CDATA[$P{datasource}]]></dataSourceExpression>
   <subreportExpression><![CDATA[$P{your sub report jrxml}]]></subreportExpression>
</subreport>

Upvotes: 1

Related Questions