Jasper Reports in java
The Jasper Reports is the world’s most popular open source reporting engine
JasperReports is an open source Java reporting tool that can write to a variety of targets, such as screen, a printer,
into PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values or XML files.
It can be used in Java-enabled applications, including Java EE or web applications, to generate dynamic content.
It reads its instructions from an XML or .jasper file.
Advantages:
Flexible and robust security
Report Execution and Scheduling
Repository
Web Services API
Source Code is available
Configurable and extensible
Professional Features.
Remarks:
Following is the description of each element mentioned below.
1.title:
Title contains the title of the report.It appears only once at the very beginning of the report, for example,
"Knowladge Hunt".
2.pageHead
PageHeader contain date and time information or organization name. This appears at top of the page.
3. columnHeader
ColumnHeader lists the names of those specific fields which you want to display in the report,
for instance, "Author Name", "Starting Hour", "Finishing Hour", "Hours Worked" and "Date" etc.
4.detail
Detail is the part where entries of the specific fields are shown, for instance "Aditya", "6:00", "14:00", "6", "10.02.2015".
5. columnFooter
ColumnFooter may display summation of any of the fields
for instance, "Worked Hours: 180".
6.pageFooter
PageFooter may contain page count information. It appears at the bottom of each page, for instance, "1/22".
Syntax of JPR.
package net.sf.dynamicreports.examples;
/* required Jar */
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;
/**
*MSQL SQL
*
CREATE TABLE `tablename` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`Address` varchar(150) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1$$
INSERT INTO `Db_name`.`tablename` (`first_name`, `last_name`, `Addrsss`) VALUES ('Bharat', 'Mishra', 'Mumbai');
INSERT INTO `Db_name`.`tablename` (`first_name`, `last_name`, `Addrsss`) VALUES ('Ram', 'Sharma', 'London');
*/
public class SimpleReportExample {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
connection = DriverManager.getConnection("jdbc:mysql://172.16.20.16:3306/Db_name","Username", "password"); // connection object using mysql DB
System.out.println("connection=="+connection);
} catch (SQLException e) {
e.printStackTrace();
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
JasperReportBuilder report = DynamicReports.report();//a new report
report
.columns(
Columns.column("Customer Id", "id", DataTypes.integerType()) // colume added on the file
.setHorizontalAlignment(HorizontalAlignment.LEFT),
Columns.column("First Name", "first_name", DataTypes.stringType()), // colume added on the file
Columns.column("Last Name", "last_name", DataTypes.stringType()), // colume added on the file
Columns.column("Address", "Address", DataTypes.stringType()) // colume added on the file
.setHorizontalAlignment(HorizontalAlignment.LEFT)
)
.title(//title of the report
Components.text("SimpleReportExample")
.setHorizontalAlignment(HorizontalAlignment.CENTER))
.pageFooter(Components.pageXofY())//show page number on the page footer
.setDataSource("SELECT id, first_name, last_name,Address FROM tablename", connection);
try {
report.show();//show the report
report.toPdf(new FileOutputStream("D:/report/report.pdf")); //export the report to a pdf file
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Cheers :)
JasperReports is an open source Java reporting tool that can write to a variety of targets, such as screen, a printer,
into PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values or XML files.
It can be used in Java-enabled applications, including Java EE or web applications, to generate dynamic content.
It reads its instructions from an XML or .jasper file.
Advantages:
Flexible and robust security
Report Execution and Scheduling
Repository
Web Services API
Source Code is available
Configurable and extensible
Professional Features.
Remarks:
Following is the description of each element mentioned below.
1.title:
Title contains the title of the report.It appears only once at the very beginning of the report, for example,
"Knowladge Hunt".
2.pageHead
PageHeader contain date and time information or organization name. This appears at top of the page.
3. columnHeader
ColumnHeader lists the names of those specific fields which you want to display in the report,
for instance, "Author Name", "Starting Hour", "Finishing Hour", "Hours Worked" and "Date" etc.
4.detail
Detail is the part where entries of the specific fields are shown, for instance "Aditya", "6:00", "14:00", "6", "10.02.2015".
5. columnFooter
ColumnFooter may display summation of any of the fields
for instance, "Worked Hours: 180".
6.pageFooter
PageFooter may contain page count information. It appears at the bottom of each page, for instance, "1/22".
Syntax of JPR.
package net.sf.dynamicreports.examples;
/* required Jar */
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.Columns;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.exception.DRException;
/**
*MSQL SQL
*
CREATE TABLE `tablename` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`Address` varchar(150) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1$$
INSERT INTO `Db_name`.`tablename` (`first_name`, `last_name`, `Addrsss`) VALUES ('Bharat', 'Mishra', 'Mumbai');
INSERT INTO `Db_name`.`tablename` (`first_name`, `last_name`, `Addrsss`) VALUES ('Ram', 'Sharma', 'London');
*/
public class SimpleReportExample {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
connection = DriverManager.getConnection("jdbc:mysql://172.16.20.16:3306/Db_name","Username", "password"); // connection object using mysql DB
System.out.println("connection=="+connection);
} catch (SQLException e) {
e.printStackTrace();
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
JasperReportBuilder report = DynamicReports.report();//a new report
report
.columns(
Columns.column("Customer Id", "id", DataTypes.integerType()) // colume added on the file
.setHorizontalAlignment(HorizontalAlignment.LEFT),
Columns.column("First Name", "first_name", DataTypes.stringType()), // colume added on the file
Columns.column("Last Name", "last_name", DataTypes.stringType()), // colume added on the file
Columns.column("Address", "Address", DataTypes.stringType()) // colume added on the file
.setHorizontalAlignment(HorizontalAlignment.LEFT)
)
.title(//title of the report
Components.text("SimpleReportExample")
.setHorizontalAlignment(HorizontalAlignment.CENTER))
.pageFooter(Components.pageXofY())//show page number on the page footer
.setDataSource("SELECT id, first_name, last_name,Address FROM tablename", connection);
try {
report.show();//show the report
report.toPdf(new FileOutputStream("D:/report/report.pdf")); //export the report to a pdf file
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Cheers :)
Comments
Post a Comment