Jasper Report run different query based on different dynamic values - jasper-reports

I have three different queries and want to run them as per value get in jasper report.Queries are written in Jrxml file itself.How I can run different queries based on different dynamic values.
Like (It is just a sudo code)
If($(a) == "Germany")
run query 1
If($(a) == "India")
run query 2
Any help would be highly appreciated.

Define parameter $P{a} in mainReport.jrxml
Make subreports subreport1.jrxml (query1), subreport2.jrxml (query2)
Put subreport1 and subreport2 into Title band of mainReport.jrxml
Use PrintWhenExpression in mainReport for properties of subreports (Window->Properties)
Set PrintWhenExpression in mainReport for subreport1: $P{a}.equals("Germany")
Set PrintWhenExpression in mainReport for subreport2: $P{a}.equals("India")
Pass $P{a} into mainReport from your application

You can try using DynamicReports. This is a library based on JasperReports, and let's you construct your report directly from you java code.
Here is an axample Dynamic Reports
There is a .setDataSource() method which can take a string + a connection objects. Based on some little logic you can set the query there.
.setDataSource() is overloaded, and it can even take a ResultSet object. So you can even use a Statement/PreparedStatement object passing different queries and values, and then passing their result as a source for the report.
Hope it helps!
P.S: The example uses maven dependencies, but you can also download the .jar and add it to your classpath to make it work

Related

Use SQL Select directly in CR Function

There's a working report but I wan't to change the visibility of certain rows based on a completely new DB select that should be executed when the report is created.
It would be ideal, if I could load the values of said select in an array or a list and than simply trigger certain row's visibility by comparing e.g. the Row Id with the values in the array.
Im used to solve a problem like this by creating a View that delivers all the essential information in each row and is used as the main data source but I was wondering if there's an elegant way within crystal reports to solve such a task.
I can think of three ways to include control data like this into the report:
One row of config data: If you can arrange it that your config data query returns one row of data, it can be just added to the data sources of the main report, without any links to tables and views already there.
Config resultset for matching: If you have to match main data results to config values row by row, e.g. by the Row Id you mentioned, add this config query to your report and link it to the main data source accordingly. (You are probably already doing this within your pre-created view on database side.)
Query config by subreport: Most flexible but also time consuming option is to add a subreport in report header, add the config data query and arrange config results into (shared) variables as needed in the subreport. Shared variable values can be used in main report then to control section visibility.
Yes, one of the 3rd-party Crystal Reports UFLs (User Function Libraries) listed here provides such a function.

How to make my subreport access a different database than the main one?

I am evaluating JasperReports and Jaspersoft Studio for using in the company I work for and I am trying to build a report that contains information from 2 different databases.
I have 2 data adapters configured.
I've my research and found out that the only way to do so is to have either a list, table or subreport in a main report, but I still can't make it work.
I took the subreport approach, I have two reports that work separately, the main one executes this query in the sample DB
select 1 from ORDERS limit 10
The subreport runs this one in a Vertica database I own:
select 1 from my_schema.my_table limit 10
Both of them, as said, run separately.
So I go to the mainreport and add a SubReport element to the ColumnHeader band, then the wizard opens:
In the step 1 I select the subreport from the project
Second step is connection. I have these options:
Same JDBC conection
Use another connection
Use an empty data source
Use a JRDatasourceExpression
Don't use any connection.
The first and third ones are not what I want.
I tried to set the second one, but I could not find a way to select the data adapter that I have configured.
The last one doesn't work ( I get a message saying that my_schema doesn't exists, so I think that it is still trying to access the MainReport database).
Is there anyway I can make a subreport run a query in a different database/datasource from the main report?
I can summarize what I did if anyone has the same issue:
I made a proof of concept (therefore I do not have the code anymore), but I created a JRDataSource class using this tutorial and there I manually access the database and returned the rows. Then I use this as the datasource of my subreport/table.
Not as nice as I wanted, but it is possible
I guess this iReport article is also applicable to reports designed in jaspersoft studio; you just need to define the subreport connection parameter with the given expression.
Perhaps, you can ask for help from two parameters which defined in the master report by yourself. Such as:
$P{MySubreport} with type net.sf.jasperreport.engine.JasperReport;
$P{MyDatasource} with type net.sf.jasperreport.engine.JRDataSource;
Associate these two parameters to your subreport element respectively in attributes 'Expression' and 'Data Source Expression';
Please write your DataSource class that implements interface JRDataSource.
Then, in your servlet class, put your real parameter values(JasperReport for your subreport .jasper file, and your DataSource object) into a parameters Map object, and call JasperFillManager.fillReport().

Jasperreport parameter selection from SQL query

I wanted to create a JasperReport which asks the user for a deliverer before it creates a report with data of the last the deliveries of the selected deliverer.
The problem, how I can do that? I found only examples where a parameter is created and the ArrayList is filled statically. But in my parameter I need the query result over the table of deliverers filled.
Can anybody tell me a possible solution?
I'm using Jaspersoft Server 5.1 which directly access a PostgreSQL database which has a datawarehouse structur. So it's not possible to use Java code.
Here is the documentation for adding input controls to reports in JasperReports Server:
http://community.jaspersoft.com/documentation/jasperreports-server-user-guide/adding-input-controls
Scroll down to the section that deals with query-based input controls. Your input control will have a name, such as deliverer. In your report, you must add deliverer as a parameter and reference it in your query, as you have done in your comment.

Two SQL query inside single JasperReport and populating Data

We are using reports in our web application.
The report is generated using JasperReports.
The problem I am facing is:
My SQL query fetches data based on a where clause:
SELECT * FROM table WHERE level='c'
I can easily show this information inside the iReport.
But I need to fire another query where level='d' and the information for the same needs to be appended to the report with that of level='c'.
I tried grouping both the outputs. But problem is how do I fire two different query while generating the report because I can write only one query in QueryBuilder inside the iReport.
Is there anyway of achieving the same.
In essence:
The report should look like:
Level=C
Name Age Phone number
Level=D
Name Age Phone number
Level D should appear only after level c is completed.
Can anyone please guide.
I finally managed to achieve the same..Grouping the data using level_id..
Initially I was trying to group in a different manner and hence the value was not getting shown..
The same is explained pretty nicely in ireport Ultimate guide-3 document.

Crystal Reports dynamic parameters values

I have crystal report with dynamic parameter (for example, linked on table COUNTRIES in some datebase).
How can i programmatically get allowed values for this parameter (list of countries)?
For static i can get default values using :
ReportDocument rd = new ReportDocument();
rd.Load(reporthPath);
rd.ParameterFields; // contains params with default values collections
Dynamic parameters has no items in DefaultValues collection. I can retrieve existing connections for report, but where i can get relation between connection and dynamic parameters?
It's not possible to do that in Crystal Reports. It's better to not use dynamic parameter field; they make your report unnecessary slow. Instead convert them to a static parameter an fill the default values from your code; that's much faster plus you know what the accepted values are.
the above answer is incorrect - as of Crystal Reports XI (at least that's where my information comes from) it is possible; see
http://devlibrary.businessobjects.com/BusinessObjectsXIR2/en/en/CrystalReports_dotNET_SDK/crsdk_net_doc/doc/crsdk_net_doc/html/crtsktutorialsrdparametersdiscrete.htm, particularly parts 2(Creating a Report with Parameters) and 5(Create a ListBox Control that Displays Default Parameters)
basically the list of possible values can be taken from the default values list,
thanks,
MD-Tech
Improving on the existing answers a little bit after running this problem to ground myself:
A ParameterField with a Static list of values has these values as items in its DefaultValues collection. The problem is that a ParameterField with a Dynamic list does not have these values pre-populated and the API does not seem to have a mechanism to query them.
A workaround involves using a Static list instead of a Dynamic one, but there may be hundreds of acceptable values which may change over time. Unless remembering to change the report(s) is appealing, a Dynamic list makes sense in this context.
In addition, using the CrystalDecisions.ReportAppServer.DataDefModel.ParameterField reveals a property BrowseField. BrowseField has properties TableAlias and Name which you can query with ADO.NET. However, BrowseField is only populated for Static parameters; it is null when using a Dynamic parameter. This is currently documented as a "needs fixed" on SAP's knowledge base, Article 2114469.
So, once you somehow get the table and column name, use ADO.NET to query the database and stick those values into the DefaultValues collection of the Dynamic parameter. Consider using a naming convention for the parameters to make this easier. Perhaps use a static parameter that isn't used in the report to house your table and column information.