lunes, 16 julio 2007

Getting started with JasperReports // Printing reports from your java app (Part III)

In the past 2 entries (Part I and Part II) we learnt how to develop a basic report and how to fill it with data from a database with a jdbc connector. Now we will learn how to call a report from within a Java application and how to set some basic parameters.

The most important thing when running your reports from within a java application is to have all the necessary jar libraries available from your classpath. The basic ones for running the sample report will be commons-logging-*.jar, commons-collections-*.jar, jasperrerports-*.jar and yourJDBCconnector.jar. All of them (except the jdbc connector) can be found in the iReport ./lib directory.

Next thing you'll need is a compiled version of your report. This you can find in the same folder you saved your report when you developed it in iReport. It will have the same name as the report source but the extension will be .jasper.

Now we are ready to call our report from Java. The most important class now is JasperPrint. The basics for this class are to specify a report and a datasource to fill it. In the following lines you'll find the code to run our report. The example simply creates a postgresql datasource and gives the path to the compiled jasper file. Then with some simple parameters it specifies the number of copies and tells the program to print the report.

try {
            /* Number of copies to print */
            int numberOfCopies = 1;
            /* Create the jdbc datasource
             * you'll have to change this code to your database vendor specific 
             * jdbc */
            Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres?user=postgres&password=PASSWORD1");
            ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM pg_tables ORDER BY 1");
            /* Create a Jasper DataSource */
            JRDataSource ds = new JRResultSetDataSource(rs);
            /* Fill the report with the new datasource */
            JasperPrint print = JasperFillManager.fillReport("C:\\Documents and Settings\\Administrador\\Mis documentos\\test.jasper", new HashMap(), 
                ds);
            PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
            /* Set the number of copies */
            printRequestAttributeSet.add(new Copies(numberOfCopies));
            printRequestAttributeSet.add(new JobName("Job Name", null));
            
            net.sf.jasperreports.engine.export.JRPrintServiceExporter exporter;
            exporter = new net.sf.jasperreports.engine.export.JRPrintServiceExporter();  
            exporter.setParameter( JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter( JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
            exporter.setParameter( JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
            exporter.setParameter( JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
            exporter.exportReport();
            rs.close();
            conn.close();
            rs = null;
            conn = null;
            exporter = null;
            print =null;
            printRequestAttributeSet = null;
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (JRException ex) {
            ex.printStackTrace();
        }

Technorati Tags:

Posted by admin at 7:59 AM in Java

Google
 
« July »
SunMonTueWedThuFriSat
1234567
891011121314
15161718192021
22232425262728
293031