Home
JoSQL and JasperReportsJoSQL and JasperReports
As of version 0.7 JoSQL now contains a External site, opens in new window Jasper Reports JRDataSource implementation that allows you to use a Query object as the data source for a JasReport report. In essence this means that you can now report on ANY List of Java objects, performing complex searches, ordering, grouping and so on.

To do this all you have to do is place an alias against each of the columns in the SELECT clause, thus:
SELECT formatDate ('lastModified') AS date,
       formatNumber (length) AS length
FROM   java.io.File
WHERE  name $LIKE '%.html'
AND    lastModified > toDateMillis ('01-Dec-2004')
ORDER BY lastModified DESC
To then integrate this with JasperReports:
JoSQLJRDataSource jds = new JoSQLJRDataSource ();

// Parse the query (from above).
jds.parse (query);
  
// Execute the query with a List of Files.
// At this point the data source will hold a reference to the results.
// If you wish to 
jds.execute (files);

InputStream reportStream = this.class.getResourceAsStream ("file-report.xml");

JasperDesign jasperDesign = JRXmlLoader.load (reportStream);
JasperReport jasperReport = JasperCompileManager.compileReport (jasperDesign);

JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport,
                                                        null,
                                                        jds);
            
JasperExportManager.exportReportToPdfFile (jasperPrint, 
                                           "file-report.pdf");  

// Very important to "clear" the results after usage otherwise references to
// objects will be "left around".
jds.clearResults ();
And of course the advantage here is that (as shown in the query) you can "pre-process" the columns before producing the report!