JasperReports with Hibernate - Module 2

Continuation of the previous blog .. JasperReports with Hibernate - Module 1


STEP 1: 

    • Install Ireports plugins in Netbeans. Please download the zip file from the below link.
    • JasperReports plugins for Netbeans
    • After its downloaded , just extract the files , you must find 4 files named as below
      • iReport-nb-3.5.2.nbm
      • jasperreports-components-plugin-nb-3.5.2.nbm
      • jasperreports-extensions-plugin-nb-3.5.2.nbm
      • jasperserver-plugin-nb-3.5.2.nbm
    • Now go to your Netbeans IDE,click on Tools menu -> plugins
    • Click on the tab named "Downloaded"
    • Click on the add plugins and browse to the folder where you extracted the .nbm files
    • Below is a screen shot of the above action

    • Select all the .nbm files and install it.Just bypass the security warnings and install it.After Installation is over, restart your Netbeans IDE.
    • Congrats , you have installed this plugin and now we will concentrate on designing Reports using this plugin. 
STEP 2 :

      We are going to give the Report a Hibernate datasource, since we have the hibernate.cfg.xml coded in the previous module, we can provide the same connection to our JasperReports (iReports).To do that please clean and build the project file which we worked out in the previous blog. 

To configure Hibernate with JasperReports , we need to give the classpath informations to the iReport plugin.These class path informations are the package where iReports can find your bean classes, mappings and hibernate.cfg.xml file, database jar file. 

Follow the below steps to accomplish the above said
    • Go to Tools-->Options
    • Click on iReport tab
    • Under iReport tabs, select the "Classpath" tab
    • Click on Add Jar and locate the suitable database jar file. Here i chose sqljdbc.jar for my MSSQL server 2005 database
    • Click on Add Folder and point it to the classes folder which serves as the root for your .java ,  .cfg.xml and .hbm.xml files
    • Check the Reloadable property of both the paths, Click ok and Restart your Netbeans again.
    • Below is the screen shot of the above procedure




STEP 3:

After you restart your Netbeans IDE, click on the "Report Datasources " icon in your tool bar, you can see that icon in the below screenshot marked by an orange line.

    • A window named Connections/Datasources will be opened, click on the New button on the right hand side.
    • Now another window named Datasource will be opened. Select "Hibernate connection" from the list and click next
    • Give the connection a suitable name and click on the Test button to check the connection.





    • You must get a "Connection test successful!" message, else please check the classpaths once again.
    • Save the connection

STEP 4:

          Come back to your StudentReports Project ,Right click on Default package, and create a new "Empty Report file "


    • Give a suitable name to the File and click ok. 
    • A new Report file named StudentReports.jrxml will be opened in an iReport Editor

    • Click on the Report query icon in the Report [ marked by an arrow ]
    • Select Query Language as "Hibernate Query Language" from the drop down list



    • Enter the following query in the query editor
    • "from Student student order by student.department.name asc"

    • The fields of the resulting query will be shown in the right hand side pane.
    • Double click on the Department property and select its name,also select fields firstName,lastName and email
    • Add the selected fields by clicking on the "Add selected field(s)" button
    • You will see the selected fields in the bottom pane
    • Now these are the fields you will be giving as input to your reports
STEP 5:

    • Keep the Report Inspector window and Palette window opened simultaneously as below

    • Expand the Fields icon in the Report Inspector, you will see all the fields to be used in the Report which u chose from the Report Query editor
    • Drag the fields you want to display in the Report into the "Details" band of the Report.
    • Drag some static text fields from the Palette into the "Column header" band and enter the appropriate field's name

    • Click on the Preview button in the Report and you will see something like below.

    • That is all !  Compile the Report and save the .jasper file for Future use

   

In the next blog , i will explain the following
    • Grouping the Records based on some common property
    • Storing generated reports into files
    • Displaying Reports in a browser using servlets

Thanks for Reading this blog!




No comments:

Post a Comment

Was this Blog Helpful?