Jasperreports integration
Jasperreports is a wonderful Reporting library/product. It's OpenSource and great for commercial products. There are several products like JasperReports Server and Jaspersoft Studio. Especially the Jaspersoft Studio is very useful for creating reports with a WYSIWYG editor. Simply use the tool and create your reports.
The most insteresting part is the integration of Jasperreports in a JVx application. It's super easy to start Jasperreports programmatically:
DBAccess dba = DBAccess.getDBAccess("jdbc:hsqldb:hsql://localhost/personsdb", "sa", "");
dba.open();
HashMap<String, Object> hmpParams = new HashMap<String, Object>();
hmpParams.put("ID" , Integer.valueOf(1));
//external connection as parameter
hmpParams.put("REPORT_CONNECTION", dba.getConnection()); // String
// compile report
JasperReport jasperReport = JasperCompileManager.compileReport(ResourceUtil.getResourceAsStream("Leaf_Grey.jrxml"));
// fill report (connection as parameter)
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmpParams);
// fill report (hardcoded connection)
//JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, hmpParams, dba.getConnection());
File fiPdf = File.createTempFile("report", ".pdf");
// export report to PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, fiPdf.getAbsolutePath());
// JVx usage
RemoteFileHandle rfh = new RemoteFileHandle(fiPdf);
// open with PDF viewer
//FileViewer.open(fiPdf);
Our example was created with Jasperreports 6.3.1. The library has some dependencies:
- Apache Commons Logging
- Apache Commons Digester 2
- Apache Commons Collections
- Apache Commons BeanUtils
- iText 2.1.7 (unpatched) or iText 2.1.7.js5 (jaspersoft patched)
A complete Eclipse example project can be found here. It connects to a HSQLDB with following tables:
(
ID INTEGER IDENTITY,
PLZ VARCHAR(5) NOT NULL,
ORT VARCHAR(100) NOT NULL,
CONSTRAINT UK_POST_PLZ_ORT UNIQUE(PLZ, ORT)
)
CREATE CACHED TABLE STRASSEN
(
ID INTEGER IDENTITY,
NAME VARCHAR(200) NOT NULL,
CONSTRAINT UK_STRA_NAME UNIQUE(NAME)
)
CREATE CACHED TABLE ADRESSEN
(
ID INTEGER IDENTITY,
POST_ID INTEGER NOT NULL,
STRA_ID INTEGER NOT NULL,
HAUSNUMMER INTEGER NOT NULL,
STIEGE INTEGER,
TUERNUMMER INTEGER,
CONSTRAINT FK_ADRE_POST_ID FOREIGN KEY (POST_ID) REFERENCES POSTLEITZAHLEN (ID),
CONSTRAINT FK_ADRE_STRA_ID FOREIGN KEY (STRA_ID) REFERENCES STRASSEN (ID)
)
CREATE CACHED TABLE ANREDEN
(
ID INTEGER IDENTITY,
BEZEICHNUNG VARCHAR(20) NOT NULL,
CONSTRAINT UK_ANRE_BEZEICHNUNG UNIQUE(BEZEICHNUNG)
)
CREATE CACHED TABLE TITEL
(
ID INTEGER IDENTITY,
BEZEICHNUNG VARCHAR(20) NOT NULL,
CONSTRAINT UK_TITE_BEZEICHNUNG UNIQUE(BEZEICHNUNG)
)
CREATE CACHED TABLE PERSONEN
(
ID INTEGER IDENTITY,
PERS_ID INTEGER,
ANRE_ID INTEGER NOT NULL,
TITE_ID INTEGER,
ADRE_ID INTEGER,
VORNAME VARCHAR(100) NOT NULL,
NACHNAME VARCHAR(100) NOT NULL,
GEBDAT DATE NOT NULL,
CONSTRAINT FK_PERS_ANRE_ID FOREIGN KEY (ANRE_ID) REFERENCES ANREDEN (ID),
CONSTRAINT FK_PERS_TITE_ID FOREIGN KEY (TITE_ID) REFERENCES TITEL (ID),
CONSTRAINT FK_PERS_PERS_ID FOREIGN KEY (PERS_ID) REFERENCES PERSONEN (ID),
CONSTRAINT FK_PERS_ADRE_ID FOREIGN KEY (ADRE_ID) REFERENCES ADRESSEN (ID)
)
The final step for the integration into a JVx application is the integration in a life-cycle object, e.g.
{
JasperReport jasperReport = JasperCompileManager.compileReport(
ResourceUtil.getResourceAsStream("person.jrxml"));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
null,
getDBAccess().getConnection());
File fiPdf = File.createTempFile("report", ".pdf");
// export report to PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, fiPdf.getAbsolutePath());
return new RemoteFileHandle(fiPdf);
}