Jasper Reports and Data Sources

March 10, 2011

Tools & IDEs

«»

Using a Database [JDBC connection] : Creating report suing iReport alone

We will be using the same table to retrieve data from and the template created for the earlier sample for generating the report.[Step 2]

And our Java Code will look like the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.sql.Connection;
import java.io.FileNotFoundException;
import java.sql.DriverManager;
import java.util.HashMap;
import net.sf.jasperreports.engine.*;
 
public class JasperJDBCConnection {
 
    public static void main(String[] args) throws FileNotFoundException, JRException {
        JasperReport jasperReport = null;
        String path = "D:/JasperTemplates/";
        JasperPrint jasperPrint = null;
 
        //Gettign the connection object
        Connection conn = getConnection();
 
        //Provide path for your JRXML template.
        String templateName = path + "ReportSQL.jrxml";
 
        //Provide path for your final pdf file.
        String destinationFile = path + "ReportSQL.pdf";
 
        //Compiling the template.
        jasperReport = JasperCompileManager.compileReport(templateName);
 
        //Sending a parameter with the logged in user name as value
        HashMap parameters = new HashMap();
 
        // Filling the report template with data
        jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
 
        //Sending a Complete print of the report.
        JasperPrintManager.printReport(jasperPrint, true);
 
        //Exporting it to an PDF
        JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);
 
    }
  //Getting the JDBC Connection object to be used in the template.The data base used for the sample is MYSQL running on localhost at port 3306
    public static Connection getConnection() {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "testjasper";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return conn;
 
    }
}
Brief explanaition of the code.
  1. ReportSQL.jrxml is the name of the JRXML template.
  2. ReportSQL.pdf is the output pdf file.
  3. getConnection() is a utility method used to get the connection object that is later used for retrieving data from the database. “testjasper” refers to the MYSQL data base running on the localhost port 3306.
  4. JasperCompileManager.compileReport() takes in the template file and converts it into a jasper object. This is later used by the reporting engine to create the report.
  5. JasperFillManager.fillReport() uses the compiled template [jasper object] ,hashmap[a dummy: ideally used to pass parameters],and the connection object created to Fill the report.
    The Connection object is used by the Jasper Reporting engine to fire the Report Query and get the data that is required to be filled in the report.
  6. Finally the Report is exported to a PDF file using the JasperExportManager.exportReportToPdfFile() method
    Output will be exactly the same; since we are using the same data and the template. The only diffrence we are making is instead of iReport invoking the Jasper Api’s internally we are doing it programmatically.

  7. XML file data source

    Lets now focus on using XML flat file as a data source instead of using a fully fledged data base.
    This feature allows jasper Report to integrate with non java applications which provide the xml data that needs to be reported.

    In this sample we will be using the same template we created earlier [with slight modifications] and use a static xml file as a data source.

    Our XML file will be:

  8. The change needed in the template file will be for the Fields.
    • The only change is that we added description.
      And our Java Code will look like the following

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      
      import java.io.FileNotFoundException;
      import java.util.HashMap;
      import net.sf.jasperreports.engine.*;
      import net.sf.jasperreports.engine.data.JRXmlDataSource;
      import net.sf.jasperreports.view.JasperViewer;
       
      public class XMLDataSourceSample {
       
          public static void main(String[] args) throws FileNotFoundException, JRException {
              JasperReport jasperReport = null;
              String recordPath = "/employee/person";
              String xmlFileName = "Address.xml";
              String path = "D:/JasperTemplates/";
              JasperPrint jasperPrint = null;
       
              //Provide path for your JRXML template.
              String templateName = path+"ReportXML.jrxml";
       
              //Provide path for your final pdf file.
              String destinationFile = path+"ReportXML.pdf";
       
              //Compiling the template.
              jasperReport = JasperCompileManager.compileReport(templateName);
       
              //Sending a parameter with the logged in user name as value
              HashMap parameters = new HashMap();
              //Creating the datasource
              JRXmlDataSource jrxmlds = new JRXmlDataSource(path+xmlFileName, recordPath);
       
              // Filling the report template with data
              jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,jrxmlds);
       
              JasperViewer.viewReport(jasperPrint);
       
              //Exporting it to an PDF
              JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);
       
          }   
      }
      Brief explanation of the code.
      1. ReportXML.jrxml is the name of the JRXML template.
      2. ReportXML.pdf is the output pdf file.
      3. JRXmlDataSource takes in two arguments Path of the xml file with data and the recordPath
      4. JasperCompileManager.compileReport() takes in the template file and converts it into a jaspe object. This is later used by the reporting engine to create the report.
      5. JasperFillManager.fillReport() uses the compiled template [jasper object] ,hashmap[a dummy: ideally used to pass parameters],and the JRXmlDataSource object created from the XML file
      6. Finally the Report is exported to a PDF file using the JasperExportManager.exportReportToPdfFile() meathod

      Output will be exactly the same; since we are using the same data [from the XML] and the template.

      Congrats you have your first Report from an XML programmatically!!.
email

«»

Comments

comments