lunes, 16 julio 2007

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

« Getting started with JasperReports // JDBC connections (Part II) | Main | Updated: Displaying a jTable inside another jTable // JTable cellRenderer »

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

 

[Trackback URL for this entry]

Comment: chena at lun, 6 ago 8:30 AM

hi,
i'm using Jasper Report to generate report.i'm usin hibernate.while retrievin the data from database

for(int i=0;i<lOptionList.size();i++)
{
String Answer = (String)OptionList.get(i);
Map.put("Report_Ans",Answer);
System.out.println("lStr : "+Answer);
}
here the value is printed in the eclipse console as male n female.

And in the xml

<parameter name="Report_Ans" class="java.lang.String"/>

<detail>



<band height="100">
<textField>
<reportElement x="28" y="8" width="100" height="29"/>
<textFieldExpression class="java.lang.String">

<![CDATA[$P{Report_Ans}]]>
</textFieldExpression>
</textField>
</band>
</detail>

i'm not able to retrieve the two values in the PDF
report generating.
i tink the problem is to retrieve the two values in xml.

pls help me

Your comment:

(not displayed)
 

SCode

Please enter the code as seen in the image above to post your comment.

 
 

Live Comment Preview:

 
Google
 
« December »
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031