Crystal Reports 9
Crystal Reports 9
Crystal Reports 9
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Crystal</strong> <strong>Reports</strong> 9<br />
Creating a Java Bean to use as a Data Source<br />
Overview<br />
Contents<br />
<strong>Crystal</strong> <strong>Reports</strong> 9 Advanced Edition has the capability to use Java Beans as data<br />
sources. This technical brief discusses how to construct a <strong>Crystal</strong> <strong>Reports</strong>compatible<br />
Java Bean. Specifically, this document demonstrates how to create a<br />
Java Bean that returns a Java ResultSet containing data from the Customer table<br />
in the Xtreme Sample Database 9.<br />
INTRODUCTION............................................................................................ 2<br />
GETTING STARTED......................................................................................2<br />
Checking for the Java Beans Connectivity folder ........................................2<br />
Installing the Java Development Kit............................................................2<br />
CREATE A JAVA BEAN ................................................................................3<br />
Compiling the Java Bean source file ...........................................................5<br />
Modifying the registry..................................................................................5<br />
Creating the first registry entry .......................................................................... 6<br />
Creating the second registry entry...................................................................... 6<br />
CREATE A REPORT USING THE JAVA BEAN DATA SOURCE ...........................7<br />
FINDING MORE INFORMATION ......................................................................8<br />
Knowledge base articles ..............................................................................8<br />
Javadocs and tutorials on resultsets ............................................................8<br />
Utility ...........................................................................................................8<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 1
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
Introduction<br />
Getting Started<br />
<strong>Crystal</strong> <strong>Reports</strong> 9 Advanced Edition is the first version that has the ability to use<br />
Java Beans as data sources (this feature is also available in <strong>Crystal</strong> <strong>Reports</strong> 10<br />
Developer and Advanced Developer editions). Any compiled Java class that has<br />
a public method returning a java.sql.ResultSet object can be used as a data<br />
source.<br />
Checking for the Java Beans Connectivity folder<br />
To verify whether your installation of <strong>Crystal</strong> <strong>Reports</strong> is properly setup to report<br />
off Java Beans, complete these steps:<br />
1. In the <strong>Crystal</strong> <strong>Reports</strong> Designer, click the New button. The <strong>Crystal</strong><br />
<strong>Reports</strong> Gallery appears.<br />
2. Click As a Blank Report, and then click OK. The Database Expert dialog<br />
box appears.<br />
3. Expand the Create New Connection folder, and then search for the Java<br />
Beans Connectivity folder (see Figure1).<br />
If the Java Beans Connectivity folder does not exist, re-install <strong>Crystal</strong><br />
<strong>Reports</strong> 10 with the Java Data components. The Java Data components<br />
are available by expanding the Data Access folder in the <strong>Crystal</strong><br />
<strong>Reports</strong> Setup dialog box.<br />
If the Java Bean Connectivity folder does exist, continue with the<br />
following sections in this document to create a Java Bean.<br />
Installing the Java Development Kit<br />
Before creating a Java Bean, download and install the latest version of J2SE<br />
from the Sun Microsystems website at http://java.sun.com/j2se.<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 2<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
Create a Java Bean<br />
Figure 1 – Database Expert Dialog Box<br />
To create a Java Bean source file, complete these steps:<br />
1. Create a text file called CR9SampleDataSourceBean.java and copy the<br />
following code into this file:<br />
import java.sql.*;<br />
/**<br />
* Title: Java Bean Data Source Sample<br />
* Description: Sample application to show how to use a<br />
Java Bean as a Data Source for CR 9<br />
* Copyright: Copyright (c) 2002<br />
* Company: <strong>Crystal</strong> Decisions, Inc.<br />
* @author Erik Lemen (Technical Support)<br />
* @version 1.0<br />
*/<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 3<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
/* For information on how to use JDBC and ResultSets see<br />
Sun's tutorials and guides at<br />
<br />
This site will explain the url, Connection, Statement and<br />
JDBC-ODBC Bridge driver<br />
that are used to populate the java.sql.ResultSet<br />
*/<br />
public class CR9SampleDataSourceBean {<br />
private ResultSet resultSet = null;<br />
private Connection con = null;<br />
private String url = "jdbc:odbc:Xtreme Sample Database<br />
9";<br />
private String JDBCBridge =<br />
"sun.jdbc.odbc.JdbcOdbcDriver";<br />
private String sqlQuery = "SELECT * FROM CUSTOMER";<br />
public CR9SampleDataSourceBean() {<br />
try {<br />
//Ensure JDBC-ODBC Bridge exists<br />
Class.forName(JDBCBridge);<br />
//Create a connection to 'Xtreme Sample Database 9'<br />
ODBC DSN<br />
con = DriverManager.getConnection(url, "Admin", "");<br />
} catch (ClassNotFoundException e) {<br />
System.out.println("Check to ensure that the JDBC-<br />
ODBC Bridge driver is installed");<br />
e.printStackTrace();<br />
} catch (SQLException e) {<br />
System.out.println("SQL Exception #" +<br />
e.getErrorCode() + " : " + e.getLocalizedMessage());<br />
}<br />
}<br />
e.printStackTrace();<br />
public ResultSet getResultSet() throws<br />
java.sql.SQLException {<br />
//Create an SQL statement to execute<br />
Statement stmt =<br />
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,<br />
ResultSet.CONCUR_READ_ONLY);<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 4<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
NOTE<br />
CAUTION<br />
//Execute the select statement to populate the<br />
ResultSet<br />
}<br />
}<br />
resultSet = stmt.executeQuery(sqlQuery);<br />
return resultSet;<br />
2. Save CR9SampleDataSourceBean.java to the D:\Sample Apps\Java\Bean<br />
DataSources\ directory.<br />
Compiling the Java Bean source file<br />
Now that you have created the CR9SampleDataSourceBean.java source file,<br />
compiled it into a class file by completing these steps:<br />
1. On the command line, change the directory to C:\Java\Bean DataSources\.<br />
2. Compile the file using the javac.exe command as follows (see Figure 2):<br />
< path to javac.exe> CR9SampleDataSourceBean.java<br />
Figure 2 – Using theJavac.exe Command<br />
The CR9SampleDataSourceBean.java source file is now being compiled<br />
into a class file. When it has finished compiling, you will see two new files<br />
created in the directory: CR9SampleDataSourceBean.java and<br />
CR9SampleDataSourceBean.class.<br />
The Java Bean source file name must be the same as the class name. In this example,<br />
the file name is CR10SampleDataSourceBean.java and the class name is<br />
CR10SampleDataSourceBean.class.<br />
Modifying the registry<br />
The next step is to create two registry entries. The first registry entry points<br />
<strong>Crystal</strong> <strong>Reports</strong> to the path containing the Java Bean class files; the second<br />
registry entry points <strong>Crystal</strong> <strong>Reports</strong> to the location of the Java Virtual Machine<br />
(JVM).<br />
The following involves editing the registry. Using Registry Editor incorrectly can cause<br />
serious problems that may require you to reinstall the Windows operating system. Use<br />
Registry Editor at your own risk.<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 5<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
For information about how to edit the registry, view the Changing Keys And Values online<br />
Help topic in Registry Editor (Regedit.exe). Note that you should make a backup copy of<br />
the registry files (System.dat and User.dat) before you edit the registry.<br />
Creating the first registry entry<br />
To create the first registry entry, complete the following steps:<br />
1. Ensure that <strong>Crystal</strong> <strong>Reports</strong> is shut down.<br />
2. On the Start menu, click Run. The Run dialog box appears.<br />
3. Type regedit.exe in the Open box and then click OK. The Registry Editor<br />
appears.<br />
4. Navigate to the following subkey:<br />
HKEY_LOCAL_MACHINE\SOFTWARE\<strong>Crystal</strong> Decisions\9.0\<strong>Crystal</strong><br />
<strong>Reports</strong><br />
5. Right-click the <strong>Crystal</strong> <strong>Reports</strong> subkey, click New, and then click Key.<br />
6. Type “Database” for the name of the subkey.<br />
7. Right-click the Database key, click New, and then click String Value.<br />
8. Type “JavaBeansClassPath” as the name of the registry entry.<br />
9. Right-click JavaBeansClassPath, and click Modify. The Edit String<br />
dialog box appears.<br />
10. In the Value data box, type the path(s) to the directory (or directories) that<br />
contains the compiled Java Bean class files. If you are using more than one<br />
directory, separate them with a semi-colon. For example:<br />
C:\JavaBean\Datatype\;C:\JavaBean\Linking\<br />
Creating the second registry entry<br />
To create the second registry entry, complete the following steps:<br />
1. In the Registry Editor, navigate to the following subkey:<br />
HKEY_LOCAL_MACHINE\SOFTWARE\<strong>Crystal</strong> Decisions\9.0\<strong>Crystal</strong><br />
<strong>Reports</strong>" key<br />
2. Right-click the <strong>Crystal</strong> <strong>Reports</strong> subkey, click New, and then click String<br />
Value.<br />
3. Type “JREPath” as the name of this new entry.<br />
4. Right-click the JREPath entry, click Modify. The Edit String dialog box<br />
appears.<br />
5. In the Value data box, type the full path to the file Jvm.dll. For example:<br />
C:\j2sdk1.4.0\jre\bin\client\jvm.dll<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 6<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
Create a Report using the Java Bean Data Source<br />
You are now ready to create a report based off your Java Bean data source.<br />
Complete the following steps:<br />
1. In the <strong>Crystal</strong> <strong>Reports</strong> 10 Designer, click the New button. The <strong>Crystal</strong><br />
<strong>Reports</strong> Gallery appears.<br />
2. Click As a Blank Report and then click OK. The Database Expert dialog<br />
box appears.<br />
3. Expand the Create New Connection folder and then click the Java Beans<br />
Connectivity folder. The Java Beans Connectivity dialog box appears<br />
displaying a list of available Java classes (see Figure 3). Methods that return<br />
java.sql.ResultSet objects are added as available tables under the Java<br />
Beans Connectivity folder.<br />
Figure 3 – Available Java Classes<br />
4. Click the CR9SampleDataSourceBean class and then click the Finish<br />
button.<br />
5. Complete the remaining steps to finish creating your report.<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 7<br />
cr9_java_bean_datasource.pdf
<strong>Crystal</strong> <strong>Reports</strong> 9 Java Bean Data Sources<br />
Finding More Information<br />
Knowledge base articles<br />
Search for the following knowledge base articles on our support site:<br />
• c2011506 - Configuring <strong>Crystal</strong> <strong>Reports</strong> 10 to use Java Bean classes as data<br />
sources<br />
• c2011653 - Limitations when using crdb_javabeans.dll to report on Java<br />
Beans<br />
• c2011830 - Troubleshooting Err Msg: "Unknown Query Engine Error"<br />
when using Java Bean<br />
• c2011834 - Report appears blank when using Java Bean as data source in<br />
CR 10<br />
Javadocs and tutorials on resultsets<br />
Javadocs on the ResultSet Interface<br />
http://java.sun.com/j2se/1.3/docs/api/java/sql/ResultSet.html<br />
Tutorial on using JDBC and creating ResultSets<br />
http://java.sun.com/products/jdbc/learning.html<br />
Utility<br />
SQLCon32 utility to troubleshoot database connections<br />
http://support.businessobjects.com/communityCS/FilesAndUpdates/sqlcon3<br />
2.zip<br />
For more information and resources, refer to the product documentation and<br />
visit the support area of the web site at www.businessobjects.com<br />
www.businessobjects.com<br />
Business Objects owns the following U.S. patents, which may cover products that are offered and sold by Business<br />
Objects: 5,555,403, 6,247,008 B1, 6,578,027 B2, 6,490,593 and 6,289,352. Business Objects, the Business Objects<br />
logo, <strong>Crystal</strong> <strong>Reports</strong>, and <strong>Crystal</strong> Enterprise are trademarks or registered trademarks of Business Objects SA or its<br />
affiliated companies in the United States and other countries. All other names mentioned herein may be trademarks<br />
of their respective owners. Product specifications and program conditions are subject to change without notice.<br />
Copyright © 2004 Business Objects. All rights reserved.<br />
11/8/2004 1:49:00 PM Copyright © 2004 Business Objects. All rights reserved. Page 8<br />
cr9_java_bean_datasource.pdf