My First JasperReport

This post is targeted at users already accustomed to NetBeans IDE

/* This post is INCOMPLETE */
Phew! It was kinda hard learning to create reports with Jasper.. Dont get me wrong, it wasnt JasperReports that was tough, JasperReports makes your life very easy. However, I think JasperSoft has to think of some way of giving a basic tutorial(the help that exists on their site isnt enough). Okay! I am greedy here. I appreciate JasperSoft for having given this superb application to the developers community.

I am writing this post so that anyone who wants to begin with JasperReports can get a fair idea on how to do it. Here are the few steps I followed. Remember, this is my first time with JasperReports, so please read and use this info at your own risk.

/*AGAIN*/ /* This post is INCOMPLETE */

STEPS I FOLLOWED:

1. Download and extract JasperReports. Create new library and add the jar files in the extracted folder(in lib folder) to this library(that you may name JasperReports)

2. Download iReport plugin for Netbeans and install it through the Tools>Plugins option of Netbeans.

3. Create your Datasource

4. Add File to your project by selecting ‘Others’ and then choosing ‘Reports with Datasource’
The wizard takes you through various options where you can create your own query. You can also group the fields you like

5. A new jrxml file will be added
Open it and iReport interface for Netbeans automatically comes to life
Now you see that your ‘Fields’ have automatically come out on the adjacent template(your jrxml file in Designer view). You can preview what the report will look like when you click the “preview” tab

Note: I use Netbeans 6.5 and I had a problem when I first opened the jrxml file in Netbeans. Although I found a temporary solution to my problem, I dont know why that happened.

Problem:
(I think the problem might be because I am using Netbeans 6.5. It seems to be working fine in Netbeans6.0)
error loading the report template
net.sf.jasperreports.engine.JRException
saxparserfactoryimpl

Solution:
1. Cut and Paste, the “xerces-2.8.0.jar” file from C:\Program Files\NetBeans 6.5\ide10\modules\ext to your desktop
2. Close and Start your Netbeans

Okay! Once I solved that problem, I wrote the following lines in my “Action file”(I am using Struts framework..you can switch the file accordingly)

//get connection here and put it in a connection object say “conn”

String reportSource = getServlet().getServletContext().getRealPath(“/”) + “xml/report1.jrxml”;
//in the above line I give the location of the jrxml file(that I created using ireport plugin)

JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);

String reportDest = getServlet().getServletContext().getRealPath(“/”) + “xml/report1.html”;
//in the above line I give the location of where I would want the HTML file that will be the output of this program to be stored

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), conn);
//am passing an empty HashMap as you might have noticed

JasperExportManager.exportReportToHtmlFile(jasperPrint, reportDest);
//there!! JasperReports will export the file thus created as an HTML to the location you specified

//JasperViewer.viewReport(jasperPrint);
//the above line is not required since I am using JasperReports in a web application

Incomplete in this post:
1. Screenshots
2. Designing reports using the iReport plugin
3. Better code

to be continued…

Tags: , ,

3 Responses to “My First JasperReport”

  1. mdahlman February 17, 2009 at 5:28 pm #

    Michael, I like your post. You’ll get complete agreement from Jaspersoft that more tutorials for people would be great–both for JasperReports and for iReport (and for JasperServer!). The iReport Ultimate Guide will be updated for the NetBeans version of iReport soon; that document is very eagerly anticipated. There are already some additional iReport tutorials available for purchase already at jaspersoft.com. You can certainly expect more and more tutorials to be created and made available.

    You should probably update your post to indicate that it’s targeted for people already using NetBeans. (Or more ambitiously, you could update it to identify which issues can be ignored by someone that just downloads the standalone version of iReport.)

    One little nitpick: you tagged the article with “i_reports”, but this should be “i_report” since the tool is “iReport”.

    Anyway, it’s a helpful article. I hope you continue to add to it and to add more like it.

    Regards,
    Matt D
    Jaspersoft

  2. Michael April 7, 2009 at 6:09 am #

    Hi Matt,

    “You’ll get complete agreement from Jaspersoft that more tutorials for people would be great–both for JasperReports and for iReport (and for JasperServer!)”

    Wow! That would be really great. I appreciate the work put up by you all.

    “You should probably update your post to indicate that it’s targeted for people already using NetBeans.”

    Have done that now. Thanks for the suggestion.

    “One little nitpick: you tagged the article with “i_reports”, but this should be “i_report” since the tool is “iReport”.”

    Thanks again for pointing that out. Have done that.

  3. rtfgvb7806 April 8, 2010 at 12:07 pm #

    IS VERY GOOD..............................

Leave a Reply