A logo showing the text blog.marcnuri.com
Español
Home»Java»Getting started with JasperReports // Printing reports from your java app (Part III)

Recent Posts

  • Fabric8 Kubernetes Client 6.4.0 is now available!
  • I bought an iPad
  • Three years at Red Hat
  • Fabric8 Kubernetes Client 6.3.1 is now available!
  • Eclipse JKube 1.10 is now available!

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

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

2007-07-16 in Java / Legacy tagged BI / Business Intelligence / Jasper / Jasper Reports / Java / Reports by Marc Nuri | Last updated: 2021-02-14

In the past 2 entries (Part I and Part II) we learned 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 generate 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 becommons-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.

The next thing you'll need is a compiled version of your report. You can find this version 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 (as opposed to .jrxml).

Now we are ready to call our report from Java. The most important class now is JasperPrint. The main requirements 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 passes 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.

1try {
2  // Number of copies to print
3  int numberOfCopies = 1;
4  // Create the jdbc datasource
5  // you'll have to change this code to your database vendor specific
6  // jdbc
7  Class.forName("org.postgresql.Driver");
8  Connection conn = DriverManager
9    .getConnection( "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=PASSWORD1");
10  ResultSet rs = conn.createStatement().executeQuery( "SELECT * FROM pg_tables ORDER BY 1");
11  // Create a Jasper DataSource
12  JRDataSource ds = new JRResultSetDataSource(rs);
13  // Fill the report with the new datasource
14  JasperPrint print = JasperFillManager
15    .fillReport( "C:\\Documents and Settings\\User\\My Documents\\test.jasper",new HashMap(), ds);
16  PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
17  // Set the number of copies
18  printRequestAttributeSet.add(new Copies(numberOfCopies));
19  printRequestAttributeSet.add(new JobName("Job Name", null));
20  net.sf.jasperreports.engine.export.JRPrintServiceExporter exporter;
21  exporter = new net.sf.jasperreports.engine.export.JRPrintServiceExporter();
22  exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
23  exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
24    printRequestAttributeSet);
25  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
26  exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
27  exporter.exportReport();
28  rs.close();
29  conn.close();
30  rs = null;
31  conn = null;
32  exporter = null;
33  print = null;
34  printRequestAttributeSet = null;
35} catch (SQLException ex) {
36  ex.printStackTrace();
37} catch (ClassNotFoundException ex) {
38  ex.printStackTrace();
39} catch (JRException ex) {
40  ex.printStackTrace();
41}
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

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

  • Avatar for chena
    chena
    2007-08-06 08:30
    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

Post navigation

← Updated: Displaying a jTable inside another jTable // JTable cellRendererGetting started with JasperReports // JDBC connections (Part II) →
© 2007 - 2023 Marc Nuri