Scenario - User input(Date Range, Report Type) is taken from JSP page & passed to servlet then to java.
I want to ask whether i should -
Main concern is which approach is better in terms of performance, robustness, future changes?
Well your question is so general. What is your approach and your business model? If your query has so much complexity , It seems that it is better to use sql directly in jasper and it will be faster than using values in jasper to generate report . But in programming paradigm , there is just a one module to know about database connection , and that is your domain side of program . I'd prefer do not initialize your database connection in your jasper configuration so just use values in your jasper and map them with Hashmap. You can use from your previous connection and database module (module that uses for your entities or domain objects ) in your program and fetch the desire data and map them to your report
Use mysql query in java & then put values in jasper using Hashmap?
This sounds crazy, a pure Hashmap
is not a datasource so you will not be able to iterate on it.
Your solutions can be:
Use mysql query in java & then put values in JRDatasource, pass datasource to jasper report
Use mysql query in jasper itself & just provide data connection using java
Generate mysql query in java and pass query and data connection to jasper report
The only scenario that I would use this is when data can not be extracted in a simple way from a single query, all data is not available in query or data is already available in memory (es. users has selected rows in a table).
When developing reports this is the "normal" and probably the quickest one, tools like iReport, gives you the possibility to extract the fields from query automatically and preview the report with live data connection (you do not need to compile and run your code). This way the developer of the report is also stand-alone , very useful in large projects.
When query gets where complex (depends on multiple variables), the query can be passed directly as parameter (or multiple parameters). In the report a default expression for the query can be used to have the advantage of 2.
So normally you use metod 2.
performance : if data is already in memory 1 is quicker (no sql) otherwise you will probably only consum more memory (hence when you load a JRDataSource). However I have not done any extensive testing.
Robustness , depends more on your code then method used
future changes , 2 has the advantage of changing only in one place "the report", you do not even need to open the java project.
If my understanding is correct, application has user selected filters for reports. These filter conditions are passed to the app in HTTP Servlet Request and you want to choose between
I would suggest to align with the project's overall back-end design strategy for fetching data from DB.
If your reports are relatively simple and application already has a fully fledged ORM implementation use the second approach. This will be relatively simple in initial implementation as well as implementation of any future change requests.
If this is not the case and sql queries for generating reports are dynamic and fairly complex I would still suggest to proceed with option 2.
Either approach may not have any significant performance difference as long as application is running on a app server with decent specs. If performance is critical and current sever resources as limited I would suggest to - run reports as a separate app in a separate JVM - separate report and app db servers and use DB replication tools to sync data across them. Generating huge complex reports tends to slow down the app performance very much.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.