HOW-TO use report query

//TODO Complete this, make this test portable (use HSQL)

When using report query, then you must pass a proper connection or data-set to the engine. In case of an SQL query, a javax.sql.Connection must be provided.


If an XPATH query is used (because of an XML data source), then the query must be passed as parameter of the constructor of the JRXmlDataSource class:

e.g.: JRXmlDataSource(document, “xpath query“);

Otherwise and empty report will result.

        public DynamicReport buildReport() throws Exception {
                 * Creates the DynamicReportBuilder and sets the basic options for
                 * the report
                FastReportBuilder drb = new FastReportBuilder();
                drb.addColumn("Id", "id", Integer.class.getName(),30)
                        .addColumn("Nombre", "nombre", String.class.getName(),30)
                        .addColumn("Apellido", "apellido", String.class.getName(),50)
                        .addColumn("Password", "password", String.class.getName(),50)
                        .setTitle("Usuarios del sistema")
                        .setQuery("select * from usuario where nombre = $P{nom}", DJConstants.QUERY_LANGUAGE_SQL)

                DynamicReport dr =;

                //Note that the query has a parameter, by putting in the map
                //an item with the proper key, it will be automatically registered as a parameter
                params.put("nom", "Juan");

                return dr;

The query is passed in this line:

 .setQuery("select * from usuario where nombre = $P{nom}", DJConstants.QUERY_LANGUAGE_SQL)
  • The query is supposed to bring columns named as the indicated before when creating report columns.
  • Queries can have parameters of the form $P{myParam}, such parameter must be registered through the parameters map.By just putting the desired parameter to be used in the query in the parameters map, DJ API will register the parameter using the map key as its name.The class type of the parameter will be determined from the map value object class.
     params.put("nom", "Juan");

Later you must pass the connection when filling the report

 jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), connection ,params );

If you use some other type of query (Hibernate) you pass the Session and other properties as parameter, then call this version of the generateJasperPrint method:

 jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), params );

Refer to QueryReportTest for a working example.

Send a Message
Fork me on GitHub