Saturday, October 23, 2010

Jasper Report and Netbeans Integration

 In this solution I am referring  

Installation of the programs listed above is a prerequisite .

At first run NetBeans IDE and go to tools >> Libraries then click on New Library.. . enter a name you prefer and set the Library Type as Class Library. After creating this , the name  is enlisted in the Libraries list . Select the name from that list . You will find Classpath tab for the library to be empty. Click on Add JAR/Folder....

then go to the iReport installation directory
then find

.......\ireport\modules\ext  directory

here you will find the following JAR files

  • commons-beanutils-1.8.2.jar
  • commons-collections-3.2.1.jar
  • commons-digester-1.7.jar
  • commons-javaflow-20060411.jar
  • commons-logging-1.1.jar
  • groovy-all-1.5.5.jar
  • iText-2.1.7.jar
  • jasperreports-3.7.5.jar


Add the files to the classpath tab you opened. After that create a new Project and add the library you created. Now you are ready to fly with reporting.
( these are the basic needs for netbeans project to be compatible with reporting ).


Use iReport designer to create the .jrxml file and place it in a directory say C:\Reports\ .  For example let the filename is sample.jrxml and an output file be C:\Reports\output\result.pdf .

Then place the following code inside main() of the project you added the library. Then simply run and see what happens.

try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","hello","hello");
   
    JasperReport jasperReport = JasperManager.compileReport("C:/Reports/sample.jrxml");
   
    JasperPrint print = JasperManager.fillReport(jasperReport, new HashMap(), conn );    

    JasperExportManager.exportReportToPdfFile(print,"C:/Reports/output/result.pdf");
}

catch(JRException ex)
{
    System.out.println(ex.getMessage());
}

catch(SQLException ex )
{
    System.out.println(ex.getMessage());
}

catch(ClassNotFoundException ex )
{
    System.out.println(ex.getMessage());
}

File directories used in  the code ar specified as I assumed before the code segment.
Also change the connection string and DBMS according to yours.