SAP Knowledge Base Article - Public

1309680 - How to pass parameters to a JavaBean using Crystal Reports?

Symptom

  • How to pass parameters to a JavaBean Resultset?

Reproducing the Issue

  • Crystal Reports XI Release 2
  • Crystal Reports 2008

Resolution

Add the required parameters to the "getResultSet" function of your JavaBean source code as shown in the example below.

For Date, Time and DateTime values, use the following classes.

  • Date = java.sql.Date
  • Time = java.sql.Time
  • DateTime = java.sql.Timestamp


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;

/*
 * Title: Java Bean Data Source Sample>
 * Description: Sample to demonstrate how to create a Java
 * Bean report data source with Crystal Reports
 */
/*
 * For information on how to use JDBC and ResultSets see Sun's
 * tutorials and guides at
 * <http://java.sun.com/products/jdbc/learning.html>.
 */
public class CRSampleDataSourceBeanParam {

    private ResultSet resultSet = null;
    private Connection connection = null;
    private String connectionURL = "jdbc:odbc:Xtreme Sample Database 2005";
    private String databaseClass = "sun.jdbc.odbc.JdbcOdbcDriver";
    private String query = "SELECT * FROM `CUSTOMER`";

    public CRSampleDataSourceBeanParam() throws SQLException {
        try {
// Ensure database class exists
            Class.forName(databaseClass);
// Create a connection
            connection = DriverManager.getConnection(connectionURL, "", "");
        } catch (ClassNotFoundException ex) {
            System.out.println("Ensure that database driver class is installed.");
            ex.printStackTrace();
        } catch (SQLException ex) {
            System.out.println("SQL Exception #" + ex.getErrorCode() + " : " + ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }
    /*
     * Any function that returns a java.sql.ResultSet within
     * the Java class can be used in Crystal Reports to return
     * report data. The function can also be parameterized to
     * 'feed' in specific values to the function
     * when querying.
     */

    public ResultSet getResultSet(int num, Date date, Time time, Timestamp datetime, String s, boolean bool) throws SQLException {
// Create an SQL statement to execute
        java.sql.Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
// Execute the select statement to populate the ResultSet
        resultSet = statement.executeQuery(query);
        return resultSet;
    }
}

Keywords

KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , Problem

Product

SAP Crystal Reports XI R2