Reputation: 13
In my project I am creating a pdf report with Jaspersoft Studio (JSS). The JSS preview generates the report correctly but from Java it creates an empty report. I have put my query in my report as querystring.
Here is my Java code:
public void getPDFReport(String userId, ChecklistReport report, OutputStream stream) {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("BRANCH_CODE", report.getBranchCode());
Resource resource = resourceLoader.getResource(reportPath);
try {
JasperDesign design = JRXmlLoader.load(resource.getInputStream());
JasperReport jasperReport = JasperCompileManager.compileReport(design);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
} catch (Exception e) {
e.printStackTrace();
}
}
And here is my report xml:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="checklist_report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="DCFCL Adapter"/>
<parameter name="BRANCH_CODE" class="java.lang.String"/>
<queryString language="SQL">
<![CDATA[SELECT F.FUNC_NAME
FROM CHECKLIST C JOIN FUNCTIONS F ON C.FUNC_ID = F.FUNC_ID
WHERE C.BRANCH_CODE=$P{BRANCH_CODE}]]>
</queryString>
<field name="FUNC_NAME" class="java.lang.String"/>
<title>
<band height="71" splitType="Stretch">
<staticText>
<reportElement x="0" y="30" width="70" height="18"/>
<text><![CDATA[Branch Code:]]></text>
</staticText>
<textField>
<reportElement x="70" y="30" width="80" height="18"/>
<textFieldExpression><![CDATA[$P{BRANCH_CODE}]]></textFieldExpression>
</textField>
</band>
</title>
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="175" height="30"/>
<text><![CDATA[Function Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement positionType="Float" x="0" y="0" width="175" height="30"/>
<textFieldExpression><![CDATA[$F{FUNC_NAME}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
How to fix this issue?
Upvotes: 0
Views: 285
Reputation: 22857
You forgot to pass db connection via Java code. The engine can't build report without it.
In case you are using report with DB based datasource you have to pass DB connection to the engine.
For example, if you are having report like this:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ..>
<queryString language="SQL">
<![CDATA[SELECT attr FROM table]]>
</queryString>
you need to pass DB connection.
The example of Java code:
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
It means that you need to use method having Connection
object at signature.
For example, this one: JasperFillManager.fillReport(JasperReport jasperReport, java.util.Map<java.lang.String,java.lang.Object> parameters, java.sql.Connection connection)
In case using Jaspersoft Studio the engine doing the same behind the scenes using dataapter options.
Upvotes: 1