Cannot call scriptlet method from SQL

0

Hello,

I've written a scriptlet class that returns a simple SQL query as a string:

package com.crow.Test;
 
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
 
public class TestClass extends JRDefaultScriptlet {
    public String TestMethod() throws JRScriptletException {
        return "SELECT firstName FROM people LIMIT 10;";
    }
}

This string is then incorporated into the SQL query of my .jrxml report.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1  -->
...
    <scriptlet name="Test" class="com.crow.Test.TestClass">
        <scriptletDescription><![CDATA[]]></scriptletDescription>
    </scriptlet>
...
    <queryString language="SQL">
        <![CDATA[$P!{Test_SCRIPTLET}.TestMethod()]]>
    </queryString>
            <textField>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{Test_SCRIPTLET}.TestMethod()]]></textFieldExpression>
            </textField>
...

However, the SQL (via $P!{Test_SCRIPTLET}.TestMethod()) fails with an exception of:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'com.crow.Test.TestClass@12ebe8f.TestMethod' at line 1

I can confirm that the scriptlet is working as it should because its query is successfully displayed in a textbox if I suppress the query SQL or run the "One Empty Record" option. The exception only occurs if the SQL is issued to the DB.

Everything I've read so far indicates this config should work -- have I missed something?

Thanks in advance!

bokucrow's picture
Joined: Aug 7 2018 - 10:58pm
Last seen: 1 year 2 weeks ago

1 Answer:

1

Yes. Scriplets are called after datasource so it's not possible to use scriptlets in your sql statement.

hozawa's picture
47107
Joined: Apr 24 2010 - 4:31pm
Last seen: 2 months 2 weeks ago
Feedback
randomness