Dynamic Data Sources for JasperReport
【Question】
I have many data sources configured in Jasper server repository. I can get the report, pass parameters, run it from my application and pass the resulting PDF stream to the client.
I expect I would be able to simply switch the source database. There are predefined parameters “REPORT_DATA_SOURCE” or “REPORT_CONNECTION”. The rest-client interface allows passing only string values to them. I expect this will work:
runReportAdapter.parameter(“REPORT_DATA_SOURCE”,“/datasources/my_alternative_datasource”);
runReportAdapter.run();
But it doesn’t. No error is reported but the data is retrieved from original data source.
【Answer】
JasperReport doesn’t support dynamic data sources. One solution is to pass a url/username/password via JDBC with a parameter, but this has potential security threat (as the password is passed through the parameter). A secure alternative is to embed SPL (Structured Process Language) into the reporting tool where the SPL script dynamically connects to a data source via a parameter and returns the result to JasperReport. For example:
A |
|
1 |
=${pSource}.query(“select * from sOrder where Amount>?”,pAmount) |
In the above SPL script, both pSource and pAmount are the report parameters. The former represents a data source name and the latter means the order amount. You can connect to esProc from JasperReport via JDBC and call an SPL script in the same way as calling a stored procedure. About detailed explanation about invocation, see How to Call an SPL Script in JasperReport.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL