Accessing Crystal Reports from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Crystal Reports from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Crystal Reports. It provides bi-directional access of Java objects and COM components.

// Assumes 'com2java' has been run on \WINNT\System32\crystl32.ocx specifying the 'crystal' 
// subdirectory as the output directory, and 'crystal' as the package name. 
 
public class CrystalClient { 
  private static final String CONNECT_STRING = "DSN=YourDsn"; 
  private static final String REPORT_FILE_NAME = "c:\\temp\\YourReportFileName.rpt"; 
  private static final String SQL_QUERY = "select * from YourTable"; 
  private static final String PRINT_FILE = "c:\\temp\\YourNewReport.doc"; 
 
 
  public static void main(String args[]) throws Exception { 
    try { 
      crystal.CrystalReport report = new crystal.CrystalReport(); 
 
      report.aboutBox(); // Lets see if it was loaded properly 
 
      report.setConnect(CONNECT_STRING); 
 
      report.setReportFileName(REPORT_FILE_NAME); 
 
      report.setSQLQuery(SQL_QUERY); 
 
      report.setPrinterCopies((short) 1); 
      report.setPrinterCollation(crystal.PrinterCollationConstants.crptDefault); 
 
      report.setDestination(crystal.DestinationConstants.crptToFile); 
      report.setPrintFileName(PRINT_FILE); 
      report.setPrintFileType(crystal.PrintFileTypeConstants.crptWinWord); // Change output type if necessary 
 
      report.printReport(); 
 
    } finally { 
      com.linar.jintegra.Cleaner.releaseAll(); 
    } 
  } 
}