Example 4: Developing reports with dynamic UDS
JReport Designer supports the Dynamic UDS feature. This feature improves performance significantly by retrieving the selected fields. At runtime, an option is provided for you to pick up the columns you want to see in the report. In this way, JReport Designer generates a dynamic report according to your selection.
In this example, SQLDataSource is used to illustrate the usage and effect of the Dynamic UDS feature. Assume that you have generated SQLDataSource.class, start JReport Designer with the modified batch file (for details, see Compiling and running SQLDataSource).
- Open an existing catalog.
- In the Data tab of the Catalog Browser, expand the data source to which the UDS is to be added.
- Right-click the Parameters node and select Add Parameter from the shortcut menu.
- In the Create Parameter dialog, enter the following:
- Name: sql
- Value Type: String
- Prompt Values:
select * from employee
select salary from employee
select employeeid, employeeposition, hiredate, notes, salary, photo from employee
For details about how to create a parameter, see Creating a parameter.
- Right-click the data source node, and then select Add User Defined Data Source from the shortcut menu.
- In the Add User Defined Data Source dialog, enter the following information:
- Name: employees
- Class Name: SQLDataSource
- Parameter:
DRIVER="org.hsqldb.jdbcDriver"&URL="jdbc:hsqldb:C:\\JReport\\Designer\\Demo\\db\\JRDemo"&USER=sa&PSWD=&SQL=@sql
The UDS employees will now have been added to the catalog.
- Click on the Standard toolbar to open an existing report, and then select File > New > Page Report Tab.
- In the New Page Report Tab dialog, specify the name and layout of the report tab as required and then click Create. Here, we choose to create a table report.
- In the Data screen of the Table Wizard, check the New radio button and choose the UDS employees from the User Defined node. Then, click Next to go to the next tab.
- In the Display screen, add the fields EMPLOYEEID, employees_NOTES, employees_SALARY and HIREDATE to be displayed in the table, edit their display names to ID, Notes, Salary, Hire Date, and then click Next.
- In the Group screen, specify to group on the field EMPLOYEEPOSITION.
- Skip the Summary screen and in the Layout and Style screen, specify to display the report in the Warm style.
- Click Finish to create the report.
- Click the View tab to view the report. You will then be prompted to enter a parameter.
- Select select * from employee as the parameter, and you will see that data for all fields has been retrieved.
- Go back and run this report again. This time, choose select salary from employee. You will notice that no groups are divided and the group name changes to NULL. This is because only the field employees_SALARY has been selected this time. JReport Designer will make no reference to the employees_Position column, on which the group is based.
Note: To make a dynamic UDS work fine, if the SQL statement at runtime doesn't include all the UDS columns, you need to make sure that none of the UDS columns' properties is edited, that is the Specify Columns option in the Add User Defined Data Source dialog should be unchecked, otherwise exceptions will be produced when the SQL statement is used to generate dynamic report from the UDS.