### Using dynamic queries

Before you can use dynamic queries, you are required to first make some preparations.

1. Compile the Java file that you write and append the path of the compiled Java file with a valid path to the class path of JReport.bat.
2. Set the value of the connection property SQL Statement Creator.

   The dynamic query interface is set as a property in the JDBC connection object in a catalog.

   In JReport Designer, launch the Catalog Browser, expand the Relational node, select the JDBC connection, right-click it and select Properties from the shortcut menu to display the properties of the connection. You will then see the SQL Statement Creator property, which is used to set the real class name of the dynamic query object.

   Here are a couple of examples:

   - You implemented the interface by the class UserSQLStmtCreatorImpl, then input the property value as UserSQLStmtCreatorImpl;@para1;@para2. Para1 and Para2 are parameters used to specify criteria while creating the query.
   - School year which changes at runtime, requires to be inserted into the report template so that the input value for the runtime parameters can be passed to the dynamic query interface via QueryInfo object, in order for a corresponding SQL statement to be returned.

   Notes:

   - Binding parameters is not supported by this feature.
   - By default, you are not permitted to change the property values in the Catalog Browser. To make the property values editable, on the Catalog Browser toolbar, click the Options button . In the Options dialog, select Catalog in the Category box and uncheck forbid editing data object properties. Then, go back to the Catalog Browser, and highlight the column name that you want to edit to change or set the property values for.
3. Set the value of the query property Enable SQL Statement Creator.

   In the Catalog Browser, in the Properties sheet of a highlighted query, there is a property named Enable SQL Statement Creator, which indicates whether or not the query uses the dynamic query interface to get the result set. When it is set to true, the query can be re-generated at runtime using the dynamic query interface.

#### An illustrative example

You are provided with a demo program, SQLStmtCreatorImpl.java in `<install_root>\help\designer\samples`, which implements the dynamic query interface. This demo is for changing the table name of the query sent to the database. Specifically, when you run the report, if you enter 1 as the tableIndex parameter, the query will dynamically change to Customer1. If you do not enter anything, you will get the result set from the Customer table.

The following example explains how to compile the required files and use dynamic queries in a report:

1. Compile SQLStmtCreatorImpl.java.

   Assume that JReport Designer has been installed in `C:\JReport\Designer`, and the class files of the MappingNameFinder.java are in `C:\JReport\Designer\help\designer\samples`:

       javac -classpath c:\jreport\designer\lib\JREngine.jar;
       c:\jreport\designer\help\designer\samples SQLStmtCreatorImpl.java

   Note: To compile SQLStmtCreatorImpl.java you will need another file MappingNameFinder.java in `<install_root>\help\designer\samples`.

2. Modify the batch file JReport.bat in `<in` `stall_root>\bin` by adding the SQLStmtCreatorImpl.java path into the batch file's class path. For example,

   If SQLStmtCreatorImpl.java is in `<install_root>\help\designer\samples`, then add the class as below:

       "c:\jdk1.4.2\jre\bin\java.exe" "-Dinstall.root=C:\JReport\Designer"
       -classpath "c:\jreport\designer\help\designer\samples;
       C:\JReport\Designer\lib\report.jar;C:\JReport\Designer\lib\resource.jar;
       C:\JReport\Designer\lib\JREngine.jar;...

3. Start up JReport Designer with the modified JReport.bat file.
4. Click File > Open. In the Open Report Set dialog, click the Browse button to open the catalog file SampleReport.cat in `<install_root>\Demo\Reports\SampleReports`, then open the sample report SalesAnalysisOverview.cls.
5. Launch the Catalog Browser, expand Data Source 1 > Relational, and then select the JDBC connection.
6. Unfold the property list of the connection by clicking the Expand button on the toolbar, then set the value of the SQL Statement Creator property as SQLStmtCreatorImpl;@tableIndex. The parameter tableIndex is used to specify which table is to be selected at runtime.
7. Select the query SalesAnalysisbyRegion in the Catalog Browser, and set the value of its property Enable SQL Statement Creator to true.
8. Save the catalog and view this report.
9. The Enter Parameter Values dialog appears. When you input 1 as the value of the tableIndex, the report will run on the table Customers1, and when you input nothing as the value of the tableIndex, the report will run on the table Customers.