Jasper 动态数据源

【问题】

I have more datasources configured in Jasperserver repository. I can get the report,passing parameters, run it from my application and pass the result 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 to pass only string values to them. I expected this will work:

runReportAdapter.parameter(“REPORT_DATA_SOURCE”,“/datasources/my_alternative_datasource”);

runReportAdapter.run();

but it doesn’t work. No error is reported but the data are retrieved from original datasource.

【回答】

Jasper 本身不直接支持动态数据源,解决的方法之一是用参数传入 jdbc url/username/password,但会影响安全性(口令在在参数中传递),另一种方法是用 jasper 集成 SPL,由 SPL 根据参数动态决定连接的数据源,并将数据返回 jasper。比如:

A
1 =${pSource}.query(“select * from sOrder where Amount>?”,pAmount)

pSource、pAmount 都是报表参数,其中 pSource 代表数据源名,pAmount 代表订单金额。

Jasper 可以通过 JDBC 连接集算器,调用脚本方法和调用存储过程一样,详情参考【JasperReport 调用 SPL 脚本】。