iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate Guideone will have its own query (or data source), fields, parameters, variables, and so on. Subdatasets are only used to feed acrosstab or a chart.Each record is a set of fields. These fields must be declared in the report in order to be used, as explained in Chapter 6. Butwhat is the difference between using a query inside a report and providing data using a JRDataSource?Basically, there is no difference. In fact, what happens behind the scenes when iReport uses a query instead of aJRDataSource is that UR executes the query using a built-in or user-defined query executor that will produce aJRDataSource. There are circumstances when providing a JDBC connection to the engine and using a query defined atreport level can simplify the use of subreports.A JRDataSource is a consumable object, which means that you cannot use the same instance of JRDataSource to fill morethan one report or subreport. A typical error is trying to use the same JRDataSource object (for example, one provided to thereport as a parameter) to feed a subreport placed in the Detail band. If the Detail band is printed more than once (and normallyit is printed for every record present in the main data source), the subreport will be filled for each main record, and every timethe subreport will iterate on the same JRDataSource. This will give results only the first time the data source is used.At the end of this chapter, you will know how to avoid this kind of error, and you’ll have all the tools you need in order todecide the best way to fill your report with any of these:• A query in a language supported by UR.• A built-in data source.• A custom data source.• A custom query language with the relative query executor.11.2 Understanding Data Sources and Connections in iReportiReport allows you to manage and configure different types of data sources to fill reports. These data sources are stored in theiReport configuration and activated when needed.When I talk about data sources, you need to understand there is a distinction between real data sources (or objects thatimplement the JRDataSource interface) and connections, used in combination with a query that is defined inside the report.In addition, the term “data source” used in UR is not the same as the concept in javax.sql.Datasource, which isonly a means of getting a physical connection to the database (usually with JNDI the lookup). The data source object I refer toin the UR realm contains concrete data.Here is a list of the data source and connection types provided by iReport:• JDBC connection• JavaBean collection data source• XML data source• CSV data source• Hibernate connection• Spring-loaded Hibernate connection• Hadoop Hive data source• JRDataSourceProvider• Custom data source• Mondrian OLAP connection• XMLA connection• EJBQL connection• Empty data sourceFinally, there is a special mode to execute a report, called query executor mode, that you can use to force the report’s creationwithout passing any connection or data source to the report engine.All the connections are “opened” and passed directly to UR during report generation. For many connections, URprovides one or more built-in parameters that can be used inside the report for several purposes (for example, tofill a subreport that needs the same connection as the parent).172

Data Sources and Query Executers• The XML data source allows you to take data from an XML document.• A CSV (comma-separated values) data source allows you to open a CSV file for use in a report.• The JavaBean set data source, custom data source, and JRDataSourceProvider allow you to print data using purposelywritten Java classes.• The Hibernate connection provides the environment to execute HQL (Hibernate Query Language) queries (thisconnection can be configured using Spring, as well).• EJBQL (Enterprise JavaBean Query Language) queries can be used with an EJBQL connection.• MDX queries can be used with a native direct connection to a Mondrian server or using the standard XML/A interface tointerrogate a generic OLAP database.An empty data source is something like a generator of records having zero fields. This kind of data source is used for testpurposes or to achieve very particular needs (like static content reports or subreports).Connections and data sources are managed through the menu command Tools > Report Datasources, which opens theconfigured connections list (Figure 11-1). To set up a new data source, you can also click the Report Datasources buttonon the toolbar.Figure 11-1Report Data Sources dialogAs mentioned previously, a connection and a data source are different objects. However, from this point on, I will usethese two words interchangeably because their functions are so similar.Even if you keep an arbitrary number of data sources ready to use, iReport works always with only one source at a time. Youcan set the active data source in several ways. The easiest and most intuitive way is to select the data source from the combobox located on the tool bar. (see Figure 11-2). You can also set the active data source by selecting a data source in theConnections/Datasources dialog box and clicking the Set as default button.173

Data Sources and Query Executers• The XML data source allows you to take data from an XML document.• A CSV (comma-separated values) data source allows you to open a CSV file for use in a report.• The JavaBean set data source, custom data source, and JRDataSourceProvider allow you to print data using purposelywritten Java classes.• The Hibernate connection provides the environment to execute HQL (Hibernate Query Language) queries (thisconnection can be configured using Spring, as well).• EJBQL (Enterprise JavaBean Query Language) queries can be used with an EJBQL connection.• MDX queries can be used with a native direct connection to a Mondrian server or using the standard XML/A interface tointerrogate a generic OLAP database.An empty data source is something like a generator of records having zero fields. This kind of data source is used for testpurposes or to achieve very particular needs (like static content reports or subreports).Connections and data sources are managed through the menu command Tools > Report Datasources, which opens theconfigured connections list (Figure 11-1). To set up a new data source, you can also click the Report Datasources buttonon the toolbar.Figure 11-1Report Data Sources dialogAs mentioned previously, a connection and a data source are different objects. However, from this point on, I will usethese two words interchangeably because their functions are so similar.Even if you keep an arbitrary number of data sources ready to use, <strong>iReport</strong> works always with only one source at a time. Youcan set the active data source in several ways. The easiest and most intuitive way is to select the data source from the combobox located on the tool bar. (see Figure 11-2). You can also set the active data source by selecting a data source in theConnections/Datasources dialog box and clicking the Set as default button.173

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!