This is a fairly complex example, which executes an SQL statement and returns a result set through the JDBC API.
Below is the Java code for class definition:
|
The following explains the above code.
For example, we used HSQL DB as data source, and C:\JReport\Designer\Demo\db\JRDemo
as the hsql db path; "sa" as the user's name, "" as the password, and select * from authors
as the SQL string. The parameter string may be similar to:
DRIVER="org.hsqldb.jdbcDriver"&URL="jdbc:hsqldb:C:\\JReport\\Designer\\Demo\\db\\JRDemo"&USER=sa&PSWD=&SQL=select * from employee
If you want to use predefined parameters in the JReport catalog, you can input the parameter string as:
DRIVER="org.hsqldb.jdbcDriver"&URL="jdbc:hsqldb:C:\\JReport\\Designer\\Demo\\db\\JRDemo"&USER=@un&PSWD=&SQL=@sql
Where, @sql is a parameter predefined in the catalog, and its default value is "select * from employee". The default value of @un is "sa".
There is one class used in this example and its source code is SQLDataSource.java, which is available in <install_root>\help\samples\APIUDS\sqlUDS
. In this example, SQLDataSource will return the result set from demo hsql db. You should set up the HSQL DB path in the URL string and point it to the demo database at <install_root>\Demo\db\JRDemo
.
Copy SQLDataSource.java to <install_root>\help
, and compile it to generate SQLDataSource.class. Append the path <install_root>\help
to the ADDCLASSPATH variable of the batch file setenv.bat in <install_root>\bin
, so that SQLDataSource can be found at runtime.
After compilation, you can now add the UDS to a JReport catalog.
If you check the Specify Columns option, the column definitions list will be enabled and you can add column definitions. If you don't specify column definitions, JReport will obtain them from the result set automatically.
Here, we will not specify column definitions. Instead, we will use the default ones from the ResultSet and the ResultSetMetaData. Enter employees in the Name field and SQLDataSource in the Class Name field. In the Parameter box, enter:
DRIVER="org.hsqldb.jdbcDriver"&URL="jdbc:hsqldb:C:\\JReport\\Designer\\Demo\\db\\JRDemo"&USER=sa&PSWD=&SQL=select * from employee
Make sure that the five bold parts in the above line must be capitalized.
Now that the SQLDataSource UDS has been successfully added to the catalog, you can use it to develop reports as required. For details about how to create reports in JReport Designer, see Creating a page report.