0

I need to generate the report using iReport and I am trying to get the data from 2 different databases. I followed this article by (Bilal Siddiqui, author of JasperReports 3.6 Development Cookbook). But I am getting following error every time. Its working fine individually but when i use sub-report it starts giving me error.

Master Report :-

<?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="MasterReport" 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="projectName" class="java.lang.String">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["D:\\rEPORTS\\"]]></defaultValueExpression>
    </parameter>
    <parameter name="connectionParam" class="java.sql.Connection" isForPrompting="false">
        <defaultValueExpression><![CDATA[java.sql.DriverManager.getConnection("jdbc:mysql://localhost/liferay_6.0.6", "root", "root")]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[QUERY]]>
    </queryString>
    <field name="EMAIL" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="32" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="2" width="100" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <text><![CDATA[EMAIL]]></text>
            </staticText>
            <staticText>
                <reportElement x="126" y="2" width="100" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <text><![CDATA[Work Station Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="2" width="100" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <text><![CDATA[Work Station Type]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="100" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{EMAIL}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="271" y="0" width="200" height="100"/>
                <subreportParameter name="emailAddress">
                    <subreportParameterExpression><![CDATA[$F{EMAIL}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{connectionParam}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "WorkstationSubReport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Error filling print... Error evaluating expression :  Source text : java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/liferay_6.0.6","root","root") 

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :  Source text : java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/liferay_6.0.6","root","root") 

6
  • Did you try report with iReport?
    – Alex K
    Aug 7, 2012 at 10:11
  • Did you check the connection string with some MySQL client or with java code?
    – Alex K
    Aug 7, 2012 at 10:12
  • Yes, I am trying this with iReport Designer 4.5.0
    – Scorpion
    Aug 7, 2012 at 10:12
  • Its running perfectly fine in single report, at the time of sub-report its giving me error.
    – Scorpion
    Aug 7, 2012 at 10:13
  • I already tried to connect this with MySQL Administrator and its working perfectly fine...
    – Scorpion
    Aug 7, 2012 at 10:14

4 Answers 4

2

Try this steps for testing master report and subreport in iReport:

  1. Create Connection in iReport and test it. Don't forget to add jar file (jdbc driver) to the application (iReport in this test case) classpath.
  2. Create master report with query. Test it with iReport connection.
  3. Create subreport with query. Test it with iReport connection.
  4. Add Subreport component to the master report.
  5. Set subreport's subreportExpression and subreportParameterExpression expressions in master report.
  6. Create parameter of java.sql.Connection type in master report. Set defaultValueExpression for it. Use this parameter in the subreport's connectionExpression expression of master report.
  7. Compile subreport and master report.
  8. Test master report via Preview button in iReport.

The snippet of master report:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["<some_path>"]]></defaultValueExpression>
    </parameter>
    <parameter name="connectionParam" class="java.sql.Connection" isForPrompting="false">
        <defaultValueExpression><![CDATA[java.sql.DriverManager.getConnection("jdbc:oracle:thin:@//<server_name>:<server_port>/<SID_name>", "<user>", "<password>")]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT id, name FROM main_table]]>
    </queryString>
    <field name="ID" class="java.lang.String"/>
    <field name="NAME" class="java.lang.String"/>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="20" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="259" y="25" width="200" height="100"/>
                <subreportParameter name="masterId">
                    <subreportParameterExpression><![CDATA[$F{ID}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{connectionParam}]]></connectionExpression>

                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "several_connections_subreport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

The snippet of subreport:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="masterId" class="java.lang.String"/>
    <queryString>
        <![CDATA[SELECT details, id FROM details_table where masterId =$P{masterId}]]>
    </queryString>
    <field name="DETAILS" class="java.lang.String"/>
    <field name="ID" class="java.lang.String"/>
    <title>
        <band height="50">
            <textField>
                <reportElement x="176" y="20" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{masterId}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="125" splitType="Stretch">
            <textField>
                <reportElement x="57" y="17" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DETAILS}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
4
  • Thanks for posting the answer. But I have follow all the steps still its giving me driver not found error. I have created parameter for connection in the master report and set connectionExpression. I also added jar file in classpath in iReport. I am not getting why its giving driver not found error.
    – Scorpion
    Aug 8, 2012 at 3:48
  • Do you have "MySQL Connection" in iReport? Did you try to change the report's language to Java?
    – Alex K
    Aug 8, 2012 at 7:19
  • Yes i have mysql connection and another connection of SQLServer for 2nd database
    – Scorpion
    Aug 8, 2012 at 7:28
  • IMHO, the classpath is the source of your problems
    – Alex K
    Aug 8, 2012 at 7:49
1

Scorpion,

Solution for this issue is, first you write a query in master report, fetch the records and then define connectionParam with default value expression as

"java.sql.DriverManager.getConnection("jdbc:oracle:thin:@//<server_name>:<server_port>/<SID_name>", "<user>", "<password>")".

In your case, you defined connectionParam first and so you are unable to run master report's query in Ireport.

Thanks, Srikanth Kattam

1

Above example does not work in iReport whereas it may work in actual application. Problem is iReport does not understand dynamic loading of the database driver automatically even if it is added into ireport classpath and you try to make it as reloadable. So you need to get the established connection from your iReport.

Following is the solution for your problem.

  1. Create a connection in iReport to the database by which your subreport will be filled.
  2. Say you have given a name "HSQLDB" for your hsql database connection.
  3. Now define a connection expression in subreport component section.
  4. Use com.jaspersoft.ireport.designer.utils.Misc.getConnection("HSQLDB") as a value to it, thats it you are done.

com.jaspersoft.ireport.designer.utils.Misc.getConnection("HSQLDB") is the key to success. Remember to remove connection param created in your main report otherwise this will not produce any result.

<subreport>
<reportElement uuid="ba5a0c29-332c-4e81-b3b6-71c17d7d2c41" positionType="Float" x="0" y="182" width="364" height="72"/>
<connectionExpression><![CDATA[com.jaspersoft.ireport.designer.utils.Misc.getConnection("HSQLDB")]]></connectionExpression>
<subreportExpression><![CDATA["\\\\subreportdir\\subreport_orders.jasper"]]></subreportExpression>
</subreport>
0

Check the langage used by your page. If you use 'Javascript' it fire this error. you should put the langage page to 'Java'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.