Sponsors

Call Report from Java Application

Wednesday, November 12, 2008

Reports designed by iReport can be called/ viewed/ embedded/ included in Java application in various ways. A sample coding is given below. Before compiling and running this code the jar files located in the lib folder of iReport installation folder must be included in the classpath.

The sample code shown below uses JFrame to view the report. You can view the report in JInternalFrame or JPanel very easily just by inheriting JInternalFrame or JPanel respectively instead of extending JFrame.

The class below has two constructors. The default constructor can be used for reports without any parameter. The second one can be used for parameterized report. The parameters should be stored in a HashMap object as the key and value pairs. The key will be the name of the parameter in the report and the value will be the variable where the input is stored.





import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.io.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.view.*;

public class MyReportViewer extends JFrame
{

public MyReportViewer(String fileName)
{
this(fileName,null);
}

public MyReportViewer(String fileName,HashMap parameter)
{
super("View Report");
try
{

/* load the required JDBC driver and create the connection
here JDBC-ODBC Bridge Driver is used*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DataSourceName");

//Way 1
/*JasperDesign jasperDesign = JasperManager.loadXmlDesign(fileName);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameter, con);*/

//Way 2
/*JasperReport jasperReport = JasperCompileManager.compileReport(fileName);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameter, con);*/

/*Way 3 (Here the parameter file should be in .jasper extension i.e., the compiled report)*/
JasperPrint print = JasperFillManager.fillReport(fileName, parameter, con);

JRViewer viewer=new JRViewer(print);

Container c=getContentPane();
c.add(viewer);
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(JRException jre)
{
jre.printStackTrace();
}

setBounds(10,10,600,500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);


}

public static void main(String args[])
{
/* A sample calling */
/*HashMap param=new HashMap();
param.put("reportParameterName",valueForTheParameter);
MyReportViewer viewer=new MyReportViewer("Report File Name With Extension",param);
viewer.setVisible(true);*/
}
}

8 comments:

Flávio Araújo said...

Hi,

I´m using JasperReports and iReports plugins (version 3.5) for NetBeans (version 6.5)

But how can I import this files that you mention as below:

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.view.*;

I have been searching some information about that, but I have not success.

Best Regards,

Flávio Araújo
São José dos Campos - SP - Brazil

Piotr said...

You'd need to switch to tab "Projects" in netbeans, select your project there and expand its contents.
Then, click right mouse button on "Libraries" and select "Add JAR/Folder".
From the browser pick the jasperreports file, which you can download from jasperreports sourceforge (google it).
That is the most straightforward way. You can also try adding it as a library to NB, through Tools->Libraries.

Shafi said...

Hi Piotr,

Would you please give a sample code to call "ireport1.jasper" from an application developed with NetBeans 6.5.1. I have following parameters:

String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://MYSYS:1433"; String user = "sa";
String password = "sapass2005";

Thanks

Anonymous said...

To call MyiReportViewer created in this tutorial,
Write the following code in the try block:

String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://MYSYS:1433"; String user = "sa";
String password = "sapass2005";


Then replace

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DataSourceName");

By

Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,password);


Then in the main method write

MyiReportViewer viewer=new MyiReportViewer("ireport1.jasper");
viewer.setVisible(true);


Make sure that, the jasper file and the java class files in the same directory, otherwise include location of ireport1.jasper also in the parameter

-S- said...

Hi there. Can you show me how to view a report with 2 parameters? I have 2 parameters which are beginning date and end date. I can show a report with just 1 parameter. But with 2 parameters, I really have no idea. Help me please.. Tq :)

Shamsuddin Ahammad said...

If you have two parameters you have to call the put method twice as below:

...
Date startDate=...
Date endDate=...
HashMap param=new HashMap();
param.put("startDate",startDate);
param.put("endDate",endDate);
...

-S- said...

Thx for the answer. It took me a couple days and it's turn out so simple! :")
Btw, I have one more question if I may. I want to show a report that has different title when I give different parameter. It's like a different header for the same report. Thank you for your helping! ^^

Shamsuddin Ahammad said...

You can create parameter for your header. Drag the parameter from View | Parameters to the header band of your report. Then pass the required value to the parameter.