Passing data from main dataset to table dataset - jasper-reports

I'm creating simple report in Jaspersoft Studio 5.6.1 with one table.
Sending data to this report from Java via JRBeanCollectionDataSource.
In report I already can get this data vie fields: report-> DAtaset and Query... -> JavaBean Tab ->
in Class Name write Java class, that comes in list in JRBeanCollectionDataSource -> add selected fields
So now I can display incoming data.
BUT if I want to do it in Table - I need to create Dataset (why?) and choose 'Use same connection used to fill the master report'. Adding same fields to new dataset doesn't help, neither choosing 'Connect to domain' for dataset. No errors displayed.

I need to create Dataset (why?)
In Jasper Reports tables are loosely coupled to the main report, i.e a table requires its own dataset to deal with.
Now say, you have JRBeanCollectionDataSource ready for the report and you wish to use it to fill table with it. Table as I said requires its own datasert, does also require its own datasource.
Now you can either specify a datasource or use same connection to fill the table as that of main report.
For more on jasper report tables visit this link and on java part visit here.

i had a similar problem. I find that you just need is set in the table of your report the REPORT_CONNECTION parameter like say
jaspercomunity
in this.
and then left the value in blank. Later in your java code use a maper object for set the SQL connection to your Data Base, something like this:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=admin");
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("REPORT_CONNECTION", conn);
jasperPrint = JasperFillManager.fillReport(reportPath, hm, beanCollectionDataSource);

Related

Override sql database field values while filling a report in Jasperreports

I've the following code to fill a report in JasperReports by passing some parameters and a postgresql database connection:
JasperFillManager.fillReport(report, jasperFillManagerParameters, sqlConnection);
The database is a third-party DB and I can't make any change on it, I also can't change report template (jrxml file); But I need to modify values of some fields while filling the report (e.g. some date convertions), so the only place I can make my changes is when I fill the report using above code.
I know that I need to work with JasperFillManager (e.g. override some of its aspects) to achieve this goal, but I don't know how should I exactly do this. Any suggestion would be appreciated.

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.

JasperReports: dynamic report generation

I am new to JasperReports. I need to design a report where certain columns and rows are dynamic. For this how I have to design the report and after design i want to integrate with my application for fetching data from action class and dto. How to map data for its?
I don't know how dynamic it will be, but it may help you: http://dynamicjasper.com/
It uses JasperReports to generate your report and it also allow you to create your report on the fly.
There are 2 options to map data with the columns. First option is u can pass ur data into data Source binding with the column name as key value pair. And the second option is u can pass the obj of your dto to data source directly in list form.
Use report.setDataSource() for setting the data. In first option make sure your type of the data matches with the column name.

How to use the same datasource twice in JasperReports/iReport

I'm trying to work out how best to do reports with a chart then a table representing the same dataset. I need to overcome the positioning of the summary is at the bottom, so intend to use subreports and table-subreports. I am experimenting with two tables and a chart in one detail band.
If I set the datasourceexpression for to $P{REPORT_DATA_SOURCE} only the chart displays data (presumably the first subreport type item) and the tables are empty. Seems the data can be consumed only once?
If I use a Dataset to query the database it works however it executes the Query three times, once for each table/chart. That will be a massive overhead.
Obviously I am not doing this right but I cannot find any examples of using the same dataset more than once.
There is no simple answer so I have raised a feature request http://jasperforge.org/projects/jasperreports/tracker/view.php?id=5487
The suggested workarounds were:
implement a custom query executer to retrieve data from a cached datasource
generate a rewindable datasource based on the retrieved result set
Thanks to sanda aka shertage on the jasperforge forum for these suggestions.
An alternative solution, cloning the dataset:
http://code.google.com/p/cloning/
Cloner cloner=new Cloner();
ArrayList clone = cloner.deepClone(getSomeArrayList());
final JRDataSource ds = new JRBeanCollectionDataSource(AnotherBean);
HashMap parameters = new HashMap();
parameters.put("PARAM_A", new JRBeanCollectionDataSource(getSomeArrayList()));
parameters.put("PARAM_B", new JRBeanCollectionDataSource(clone));