Getting started with JasperReports // Printing reports from your java app (Part III)
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}
Comments in "Getting started with JasperReports // Printing reports from your java app (Part III)"
i'm using Jasper Report to generate report.i'm usin hibernate.while retrievin the data from database
here the value is printed in the eclipse console as male n female.
And in the xml
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