How to access jasper subreport from java desktop application

0
Hi,
 
I Created following reports in jasper,
 
Master.jrxml :
 
 
         <?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="Master" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\Program Files\\TMS\\reports\\"]]></defaultValueExpression>
</parameter>
<queryString language="SQL">
<![CDATA[select * from student]]>
</queryString>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="english" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="maths" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="history" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="science" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="22" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="22"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Center">
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[STUDENT DETAILS]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="31" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[name]]></text>
</staticText>
<staticText>
<reportElement x="111" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[english]]></text>
</staticText>
<staticText>
<reportElement x="222" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[maths]]></text>
</staticText>
<staticText>
<reportElement x="333" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[history]]></text>
</staticText>
<staticText>
<reportElement x="444" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[science]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="111" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{english}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="222" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{maths}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="333" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{history}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="444" y="0" width="111" height="20"/>
<box topPadding="2" leftPadding="3" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{science}]]></textFieldExpression>
</textField>
</band>
<band height="35">
<subreport>
<reportElement x="0" y="0" width="555" height="35"/>
<subreportParameter name="name">
<subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["C:\\Program Files\\TMS\\reports\\Master_subreport1.jasper"]]></subreportExpression>
</subreport>
</band>
<band height="42">
<subreport>
<reportElement x="0" y="0" width="555" height="39"/>
<subreportParameter name="name1">
<subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "Master_subreport2.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band height="3" splitType="Stretch"/>
</summary>
    </jasperReport>
 
 
Master_subreport1:
 
       <?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="Master_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="name" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[select * from perm_add where name=$P{name}]]>
</queryString>
<field name="pid" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="street" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="district" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="state" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band splitType="Stretch"/>
</title>
<pageHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<text><![CDATA[Permanent Address]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[street]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[district]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[state]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{street}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{district}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="4" bottomPadding="2" rightPadding="3">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
    </jasperReport>
 
Master_subreport2:
 
       <?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="Master_subreport2" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="name1" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[select * from temp_add where name=$P{name1}]]>
</queryString>
<field name="tid" class="java.lang.Long">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="street" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="district" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="state" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band splitType="Stretch"/>
</title>
<pageHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement>
<font size="11" isBold="true"/>
</textElement>
<text><![CDATA[Temporary Address]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="20" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[street]]></text>
</staticText>
<staticText>
<reportElement x="185" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[district]]></text>
</staticText>
<staticText>
<reportElement x="370" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<text><![CDATA[state]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{street}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="185" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{district}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="370" y="0" width="185" height="20"/>
<box topPadding="2" leftPadding="5" bottomPadding="4" rightPadding="2">
<pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
    </jasperReport>
 
 
The Main report with sub reprts are working perfectly when i use preview in ireport:
But when iam calling from java swing code iam unable to view the report.
Here is my java code:
 
            try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Subr.class.getName()).log(Level.SEVERE, null, ex);
            }
            Connection conn = null;
            try {
                conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","chennai");
            } catch (SQLException ex) {
                Logger.getLogger(Subr.class.getName()).log(Level.SEVERE, null, ex);
            }
        
            String sqlPrint1="SELECT  name , english , maths , history , science from student where name='Thomas'";
            JasperDesign jasperDesign;
            jasperDesign = JRXmlLoader.load("C:/Program Files/TMS/reports/Master.jrxml");
            JasperDesign jasperDesignSub1 = JRXmlLoader.load("C:/Program Files/TMS/reports/Master_subreport1.jrxml");
            JasperDesign jasperDesignSub2 = JRXmlLoader.load("C:/Program Files/TMS/reports/Master_subreport2.jrxml");
            JRDesignQuery newQuery = new JRDesignQuery();
            newQuery.setText(sqlPrint1);
            jasperDesign.setQuery(newQuery);
            JasperReport jasperReport= JasperCompileManager.compileReport(jasperDesign);
            JasperReport jasperReportSub1 = JasperCompileManager.compileReport(jasperDesignSub1);
            JasperReport jasperReportSub2 = JasperCompileManager.compileReport(jasperDesignSub2);
            Map perameters=new HashMap();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,perameters,conn);
            JasperViewer.viewReport(jasperPrint,false);
            } catch (JRException ex) {
            Logger.getLogger(Subr.class.getName()).log(Level.SEVERE, null, ex);
            }
 
 
 
rpradeepsai's picture
Joined: Feb 19 2014 - 10:58pm
Last seen: 3 months 4 days ago

Please help me with correct java code.

rpradeepsai - 5 years 1 month ago

0 Answers:

No answers yet
Feedback