Creating a Java class that specifies a custom data source

To use a JavaBean (or a Java class that does not extend the bean type) to specify a custom data source, you need to create a class that serves as a JavaBeans connectivity data source for creating Crystal reports.
This class needs to conform to the JavaBean standard by defining private properties that are accessible only through get and set methods. In this way, Crystal Reports can introspect the class as a Bean. The JavaBean connectivity classes require the following:
  • The constructor must have no parameters.
  • Methods that are used as tables must have a return type of ResultSet.
  • Statement objects must be created using the ResultSet.TYPE_SCROLL_SENSITIVE and ResultSet.CONCUR_READ_ONLY arguments.

Note: Methods returning ResultSet can take arguments. These arguments are treated as stored procedure parameters by Crystal Reports.
Example: 
This example implements a JavaBeans connectivity data source for an ODBC data source named MyDatabase.
public class JavaBeanConnectivityTest
{
  private Connection connection;
	 private ResultSet customerResultSet;
	
	 public JavaBeanConnectivityTest() throws ClassNotFoundException, SQLException
	 {
		  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		  String jdbcURL = "jdbc:odbc:MyDatabase";
		  connection = DriverManager.getConnection(jdbcURL);
	 }

	 public Connection getConnection()
	 {
		  return connection;
	 }
	
	 public ResultSet getCustomerResultSet() throws SQLException
	 {
		  String query = "Select [Customer ID], [Customer Name], [Last Year's Sales] from Customer";
		  Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		  customerResultSet = statement.executeQuery(query);
		  return customerResultSet;
	 }
	
	 public void setCustomerResultSet(ResultSet newResultSet)
	 {
		  customerResultSet = newResultSet;
	 }
}
This list includes the classes used by the sample code:
  • java.sql.Connection
  • java.sql.DriverManager
  • java.sql.ResultSet
  • java.sql.SQLException
  • java.sql.Statement