Pages

Wednesday, August 1, 2012

Generate Jasper Reports using Java

Hi Guys,

Here I am going to show you the code using which you can generate Simple Static Jasper Reports from simple Java Application.
First of all, to generate Jasper Reports you need to add following JAR files in your project build path:


  • commons-beanutils-1.8.0.jar
  • commons-collections-3.1.jar
  • commons-digester-2.0.jar
  • commons-logging-1.1.1.jar
  • groovy-all-1.5.5.jar
  • iText-2.1.7.jar
  • jasperreports-4.0.1.jar
  • mysql-connector-java-5.1.14-bin.jar


After adding these JARs in project build path, try out this code:

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;


public class GenerateReport {
public static void main(String args[])
{
JasperPrint jasperPrint=null;
try {
String reportPath="<PATH_TO_YOUR_REPORT_JRXML_FILE>";
String targetPath=reportPath.replace(".jrxml", ".pdf");
final JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
jasperPrint = JasperFillManager.fillReport(jasperReport,null,JDBCConnection.getJDBCConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint,targetPath);


} catch (final JRException e) {
e.printStackTrace();
}

}
}

In the above code, <PATH_TO_YOUR_REPORT_JRXML_FILE> is the path to your JRXML file i.e. "E:/report1.jrxml".
And JDBCConnection is the class, where I have write code to make database connection. Here is the code of JDBCConnnection.java


import java.sql.Connection;
import java.sql.DriverManager;


public final class JDBCConnection
{
    public static Connection getJDBCConnection()
    {
        Connection connection = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
        }
        catch(Exception e)
        {
        e.printStackTrace();
        }
        return connection;
    }
}


After running this code, you will find a generated PDF File on the same path where you put your JRXML file.
Here is the snapshot of generated PDF File.





4 comments:

  1. Hi Dhaval,

    I have used a sample jrxml file as my report file. however when i run them, i get blank pdf file. also i dont have any errors in my eclipse console. can u pls provide the jrxml file which u have used please!!

    ReplyDelete
  2. Ohkk..
    which report query you have provided??
    Can you please tell that query?

    ReplyDelete
  3. Hi Dhaval,

    This code works fine but I need to generate report from table in the DB.So I need my query has to be executed then it should be generated as report.The above code does not use query so can you help.

    Regards,
    Anand

    ReplyDelete
    Replies
    1. Hi Anand,
      First let me tell you how Jasper Reports works. When you create Jasper Reports using java program, there would be NO query in Java Class. The query will be written inside your JRXML file. In JRXML, when you design your report; to deal with database Tables you need to follow this things:
      1) Make connection with database from JRXML file.(DataSource)
      2) Create Dataset to deal with Tables in the DB. DataSet is a same kind of thing as ResultSet in JDBC. You need to mention query in DataSet.

      This way you can populate table data in JasperReport.

      If you want to generate parameterize Jasper Report. Then you can refer my post:

      http://opensourcetechno.blogspot.in/2012/08/generate-parameterized-jasper-reports.html

      Please let me know if you have concern.

      Delete