Create the Report
With all of the setup out of the way, creating the report and connecting to a JDBC-compliant data source is actually a simple, two-step process:
- Design the report in Crystal as usual
- Use a JDBC database connection from within Crystal
When setting up your report, use a JDBC/JNDI database connection. Begin by selecting "JDBC/JNDI" under the "Create New Connection" folder (either via the Report Wizard or the Database Expert). Fill out the necessary JDBC connection information in the new connection form. (Figure 1 shows an example of how to do this for an Oracle database.) Click "Next" and then supply the UserID and password for connecting to the database, if required.
|Figure 1. New JDBC Connection for an Oracle Database|
Once connected to the database, specify which tables you want to pull from for the report, which columns to include, and how the fields should be grouped/sorted (these steps are no different from how you handle standard data source definitions within Crystal).
The report is now ready to go. You can test it within Crystal and play with it to ensure that the report is formatted to your liking. Once you are satisfied with the report format and data configuration, you are ready to save the report and move on to the Java Web development and application deployment.
Write the Java Code
Once you start a JSP page, which you can do with a couple of imports:
<%@ page import="com.crystaldecisions.reports.reportengineinterface.*,
You can display a Crystal report via the JSP page in as few as four API calls to the JRC library:
CrystalReportViewer crv = new CrystalReportViewer();
JPEReportSourceFactory jrsf = new JPEReportSourceFactory();
jrsf.createReportSource( "MyReport.rpt", request.getLocale() ) );
crv.processHttpRequest( request, response, application, null );
The above code should work unaltered in your environment except for the report name (the first parameter passed into the
createReportSource() method), which you need to change to the name you gave the report when you saved it. Also, the code is compatible with both Crystal X and Crystal XI. A more advanced approach using the
ReportClientDocument API is available for Crystal XI (see the whitepaper "Crystal Reports XI for J2EE Startup Guide" (PDF) on the Business Objects developer site for details).