Describe how to use crystal reports in java.
Install and run crystl32.cox file in \WINNT\System32 folder and specify the sub directory as ‘crystal’ as the output directory.
- Create an object of CrystalReport class.
- Invoke aboutBox() method – for confirmation whether it is loaded properly
- Establish the connection by using setConnect() method.
- Specify the output file format by using setReportFileName() method.
- Execute the query by using setSqlQuery() method.
- Set the number of copies of the report and the printer collation.
- Set the destination file using setDestination() method.
- Specify the file type using setPrintFileType () method.
- Invoke the method pritReport()
The following is the code example public class CrystalReportExample
{
private static final String CONNECT_STRING = "DSN=YourDsn";
private static final String REPORT_FILE_NAME = "FileName.rpt";
private static final String SQL_QUERY = "select * from YourTable";
private static final String PRINT_FILE = "YourNewReport.doc";
public static void main(String args[]) throws Exception
{
try
{
crystal.CrystalReport report = new crystal.CrystalReport();
report.aboutBox();
report.setConnect(CONNECT_STRING); // database connection
report.setReportFileName(REPORT_FILE_NAME); // name of the report file
report.setSQLQuery(SQL_QUERY); // query to get the data from the table
report.setPrinterCopies((short) 1); // number of copies of the report
// specifying the printer collating sequence
report.setPrinterCollation(crystal.PrinterCollationConstants.crptDefault);
report.setDestination(crystal.DestinationConstants.crptToFile); // destination of the report
report.setPrintFileName(PRINT_FILE); // writing the report into the file
report.setPrintFileType(crystal.PrintFileTypeConstants.crptWinWord); // the type of the report file
report.printReport(); // printing the report onto the destination with the specified requirements
}
finally
{
com.linar.jintegra.Cleaner.releaseAll(); // releasing all report resources.
}
}
}