Connecting to Java-Based Data Sources


The COM Data Source driver is targeted at Visual Basic and Visual C++, and ADO.NET is targeted at .NET developers. Because Crystal Reports XI has a full Java SDK, an equivalent Java Data Source driver provides equivalent functionality of the COM driver for developers using the Java platform.

The process of creating a Java Data Source driver is conceptually similar to that of creating a COM Data Source driver. A Java class needs to be created that has a public function with a return type of ResultSet or CachedRowSet. A ResultSet is the standard object returned from a JDBC-based query, whereas the CachedRowSet is a disconnected recordset useful for parsing out things like XML. Listing 15.2 shows a simple Java Data Provider that returns a ResultSet.

Listing 15.2. A Java Data Provider That Returns Data from the Sample Database

[View full width]

import java.lang.*; import java.sql.*; public class XtremeDataProvider {     public ResultSet Employee()throws java.sql.SQLException     {                         ResultSet rs = null;                         Connection con = null;                         String url = "jdbc:odbc:Xtreme Sample Database 11";                         String JDBCBridge = "sun.jdbc.odbc.JdbcOdbcDriver";                         try{                                     // connect to the database                                     Class.forName(JDBCBridge);                                     con = DriverManager.getConnection(url, "", "");                                     // run a SQL query                                     Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);                                     String query = "SELECT * FROM Employee";                                     rs = stmt.executeQuery(query);         }catch (ClassNotFoundException e) {                         System.out.println("Check JDBC-ODBC bridge driver");                         e.printStackTrace();             } catch (SQLException e) {                         System.out.println("SQL Exception #" + e.getErrorCode() + " : " +  e.getLocalizedMessage());                         e.printStackTrace();             }             // return the results of the query             return rs;     } }

To identify a Java class, simply compile the code into the .class file and place that compiled .class file into the JavaBeans classpath. To define the classpath, edit the following properties in the CRconfig.xml file in the default folder: C:\Program Files\Common Files\Business Objects\3.0\java\.

JavaDir

This property must refer to a valid Java Runtime Environment (JRE) or J2SE Development Kit (JDK). If a JRE or JDK was detected during install, this property will already be set. If not, install JDK 1.4 and set the property manually. A valid setting is the complete path to the JDK:

<JavaDir>c:\Program Files\Java\JRE\bin</JavaDir>


JavaBeansClassPath

If the JavaBeans are unjarred, simply refer to the .class file's path:

<JavaBeans>     <CacheRowSetSize>100</CacheRowSetSize>     <JavaBeansClassPath>c:\myjavabean</JavaBeansClassPath> </JavaBeans>


and if the JavaBean class files are jarred, refer to the same location with the .jar extension:

<JavaBeans>     <CacheRowSetSize>100</CacheRowSetSize>     <JavaBeansClassPath>c:\myjavabean.jar</JavaBeansClassPath> </JavaBeans>


During the process of creating a report, Crystal Reports searches through all classes contained in the classpath. It then provides a list of methods with return types of java.sql.ResultSet. The same rules about function arguments apply. Any arguments to the Java method are mapped to report parameter fields. Using Java code, you can control exactly what data comes back.




Crystal Reports XI(c) Official Guide
Crystal Reports XI Official Guide
ISBN: 0672329174
EAN: 2147483647
Year: N/A
Pages: 365

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net