Cannot call scriptlet method from SQL


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 using JasperReports Library version 6.4.1  -->
    <scriptlet name="Test" class="com.crow.Test.TestClass">
    <queryString language="SQL">
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="12"/>

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: 4 years 7 months ago

1 Answer:

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

hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 3 years 9 months ago