What is a Report?
A report is a goodlooking formatted way of presenting the data that you
have entered in database. Reports are all about querying a database and
displaying the results in a goodlooking format.as like graph,charts etc.
What is Jasper Report?
Jasper Reports-the backbone of iReport is a rendering library of data,it is not
a standalone application. It cannot run directaly and must be
embedded in another client- or server-side Java application.
The Jasper Reports library is a very usefull and flexible reportgenerating
tool that delivers rich content to the screen, a printer, or a
file in PDF, HTML, RTF, XLS, CSV or XML format.
The library is written entirely in Java application and can be used in a variety of
Java-enabled software applications, including J2EE or Web applications, to
generate dynamic content. Its helps to create pageoriented,
ready-to-print documents in a simple and flexible manner.
What is iReport?
iReport is an OpenSource program that help to create complex reports.
iReport use every kind of java application through JasperReports
library. It is written in 100% pure java and it is distributed with its
source codes according to the GNU General Public License. It is a
visual tool to obtain XML files for JasperReports. It provides a
WYSIWYG environment to design reports.
You can use iReport in two different waysa.
a.Standalone installation
b. Using Plugins
If you want to use standalone installation you need to download
iReport-Designer for Jasper Reports from here. The latest version of
Jasper Reports is currently iReport-5.1.0. You can also try with any
previous version.
If you want to use plugins for NetBeans then download from here.
Current iReport NetBeans plugin version is 4.7.1.
I assume that you hava a previous knowledge on how to design a
report in iReport. First we need to design a report using iReport
which is beyond of this topics.
Now create a simple Java Application to call this report.
Note that I have already created the report with iReport 4.1.2 and
placed the .jasper file to project root directory.
To call the report you must have to include the following jars in
classpath-
1. commons-beanutils-1.8.2.jar
2. commons-collections-3.2.1.jar
3. commons-digester-1.7.jar
4. commons-logging-1.1.jar
5. groovy-all-1.7.5.jar
6. iText-2.1.7.jar
7. jasperreports-4.1.2.jar
You also need to include mysql-connector-java-5.1.13-bin.jar
Note:
Those above jar files are version specific i.e. if you have created a
JRXML/Jasper file using a particular version of iReport you must use
same set of jars. But don’t worry, you can easily get the jars from
iReport installation directory. In my cases the directory is
C:\Program Files\Jaspersoft\iReport-4.1.2\ireport\modules\ext.
Now create the main file from where you want to call the report.
import util.Database;
import java.sql.*;
import java.util.HashMap;
import report.Report;
public class TestReportCall {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
connection = Database.getConnection();
statement = connection.createStatement
HashMap parameterMap = new HashMap();
parameterMap.put("rtitle", "Report Tit
Report rpt = new Report(parameterMap,
rpt.setReportName("productlist"); //pr
rpt.callReport();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Add your database connection file to util package.
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class Database {
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver")
Connection con = DriverManager.getConn
"root", "dbpass");
return con;
} catch (Exception ex) {
System.out.println("Database.getConnec
return null;
}
}
public static void close(Connection con) {
try {
con.close();
} catch (Exception ex) {
}
}
}
Now create package called report and add the Report.java file to it.
package report;
import java.awt.Container;
import java.sql.Connection;
import java.util.HashMap;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JREmptyDataSou
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillMana
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;
import util.Database;
public class Report extends JFrame {
HashMap hm = null;
Connection con = null;
String reportName;
public Report() {
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
}
public Report(HashMap map) {
this.hm = map;
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
}
public Report(HashMap map, Connection con) {
this.hm = map;
this.con = con;
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
setTitle("Report Viewer");
}
public void setReportName(String rptName) {
this.reportName = rptName;
}
public void callReport() {
JasperPrint jasperPrint = generateReport(
JRViewer viewer = new JRViewer(jasperPrin
Container c = getContentPane();
c.add(viewer);
this.setVisible(true);
}
public void callConnectionLessReport() {
JasperPrint jasperPrint = generateEmptyDa
JRViewer viewer = new JRViewer(jasperPrin
Container c = getContentPane();
c.add(viewer);
this.setVisible(true);
}
public void closeReport() {
//jasperViewer.setVisible(false);
}
/** this method will call the report from dat
public JasperPrint generateReport() {
try {
if (con == null) {
try {
con = Database.getConnection(
} catch (Exception ex) {
ex.printStackTrace();
}
}
JasperPrint jasperPrint = null;
if (hm == null) {
hm = new HashMap();
}
try {
/**You can also test this line if
* report from any absolute path
//jasperPrint = JasperFillManager
jasperPrint = JasperFillManager.f
} catch (JRException e) {
e.printStackTrace();
}
return jasperPrint;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/** call this method when your report has an
public JasperPrint generateEmptyDataSourceRep
try {
JasperPrint jasperPrint = null;
if (hm == null) {
hm = new HashMap();
}
try {
jasperPrint = JasperFillManager.f
} catch (JRException e) {
e.printStackTrace();
}
return jasperPrint;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
Hope you are now able to call Jasper Report from your Java
application.
In fact, I am always trying my best to provide you complete working
example in every topics. Your like, share and comment are so much
appreciated.
A report is a goodlooking formatted way of presenting the data that you
have entered in database. Reports are all about querying a database and
displaying the results in a goodlooking format.as like graph,charts etc.
What is Jasper Report?
Jasper Reports-the backbone of iReport is a rendering library of data,it is not
a standalone application. It cannot run directaly and must be
embedded in another client- or server-side Java application.
The Jasper Reports library is a very usefull and flexible reportgenerating
tool that delivers rich content to the screen, a printer, or a
file in PDF, HTML, RTF, XLS, CSV or XML format.
The library is written entirely in Java application and can be used in a variety of
Java-enabled software applications, including J2EE or Web applications, to
generate dynamic content. Its helps to create pageoriented,
ready-to-print documents in a simple and flexible manner.
What is iReport?
iReport is an OpenSource program that help to create complex reports.
iReport use every kind of java application through JasperReports
library. It is written in 100% pure java and it is distributed with its
source codes according to the GNU General Public License. It is a
visual tool to obtain XML files for JasperReports. It provides a
WYSIWYG environment to design reports.
You can use iReport in two different waysa.
a.Standalone installation
b. Using Plugins
If you want to use standalone installation you need to download
iReport-Designer for Jasper Reports from here. The latest version of
Jasper Reports is currently iReport-5.1.0. You can also try with any
previous version.
If you want to use plugins for NetBeans then download from here.
Current iReport NetBeans plugin version is 4.7.1.
I assume that you hava a previous knowledge on how to design a
report in iReport. First we need to design a report using iReport
which is beyond of this topics.
Now create a simple Java Application to call this report.
Note that I have already created the report with iReport 4.1.2 and
placed the .jasper file to project root directory.
To call the report you must have to include the following jars in
classpath-
1. commons-beanutils-1.8.2.jar
2. commons-collections-3.2.1.jar
3. commons-digester-1.7.jar
4. commons-logging-1.1.jar
5. groovy-all-1.7.5.jar
6. iText-2.1.7.jar
7. jasperreports-4.1.2.jar
You also need to include mysql-connector-java-5.1.13-bin.jar
Note:
Those above jar files are version specific i.e. if you have created a
JRXML/Jasper file using a particular version of iReport you must use
same set of jars. But don’t worry, you can easily get the jars from
iReport installation directory. In my cases the directory is
C:\Program Files\Jaspersoft\iReport-4.1.2\ireport\modules\ext.
Now create the main file from where you want to call the report.
import util.Database;
import java.sql.*;
import java.util.HashMap;
import report.Report;
public class TestReportCall {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
connection = Database.getConnection();
statement = connection.createStatement
HashMap parameterMap = new HashMap();
parameterMap.put("rtitle", "Report Tit
Report rpt = new Report(parameterMap,
rpt.setReportName("productlist"); //pr
rpt.callReport();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Add your database connection file to util package.
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class Database {
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver")
Connection con = DriverManager.getConn
"root", "dbpass");
return con;
} catch (Exception ex) {
System.out.println("Database.getConnec
return null;
}
}
public static void close(Connection con) {
try {
con.close();
} catch (Exception ex) {
}
}
}
Now create package called report and add the Report.java file to it.
package report;
import java.awt.Container;
import java.sql.Connection;
import java.util.HashMap;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JREmptyDataSou
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillMana
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JRViewer;
import util.Database;
public class Report extends JFrame {
HashMap hm = null;
Connection con = null;
String reportName;
public Report() {
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
}
public Report(HashMap map) {
this.hm = map;
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
}
public Report(HashMap map, Connection con) {
this.hm = map;
this.con = con;
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE
setTitle("Report Viewer");
}
public void setReportName(String rptName) {
this.reportName = rptName;
}
public void callReport() {
JasperPrint jasperPrint = generateReport(
JRViewer viewer = new JRViewer(jasperPrin
Container c = getContentPane();
c.add(viewer);
this.setVisible(true);
}
public void callConnectionLessReport() {
JasperPrint jasperPrint = generateEmptyDa
JRViewer viewer = new JRViewer(jasperPrin
Container c = getContentPane();
c.add(viewer);
this.setVisible(true);
}
public void closeReport() {
//jasperViewer.setVisible(false);
}
/** this method will call the report from dat
public JasperPrint generateReport() {
try {
if (con == null) {
try {
con = Database.getConnection(
} catch (Exception ex) {
ex.printStackTrace();
}
}
JasperPrint jasperPrint = null;
if (hm == null) {
hm = new HashMap();
}
try {
/**You can also test this line if
* report from any absolute path
//jasperPrint = JasperFillManager
jasperPrint = JasperFillManager.f
} catch (JRException e) {
e.printStackTrace();
}
return jasperPrint;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/** call this method when your report has an
public JasperPrint generateEmptyDataSourceRep
try {
JasperPrint jasperPrint = null;
if (hm == null) {
hm = new HashMap();
}
try {
jasperPrint = JasperFillManager.f
} catch (JRException e) {
e.printStackTrace();
}
return jasperPrint;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
Hope you are now able to call Jasper Report from your Java
application.
In fact, I am always trying my best to provide you complete working
example in every topics. Your like, share and comment are so much
appreciated.
0 comments:
Post a Comment