Generate Reports in java using DynamicReports and
JasperReports
This example shows you how to generate a simple report using
DynamicReports and JasperReports
For developing this you must need to download DynamicReports the
library and include it into their classpath. It allows you to produce documents
that can be exported into many popular formats, It is based on JasperReport
library.
1 : Create a DataSource and use it into report generation as
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://hostname:port/dbname”,”username”,”password”);
2 : Suppose mysql server contains a database test having
table customer having fields
The table customer having fields
Column name
|
Data type
|
id
|
int
|
first_name
|
varchar(50)
|
last_name
|
varchar(50)
|
Created_date
|
datetime
|
age
|
int
|
We will create report
that will get all customers from the table and the data will be put into the
report.
3 :Create a empty report
object
JasperReportBuilder report = DynamicReports.report();
4: Now create a report colum for each database column
Columns.column("Customer Id", "id",
DataTypes.integerType());
Where first parameter is column level
Second parameter is actual table field which is same as
table column name.
Third parameter is datatype
5 : Add the report columns.
report
.columns(
Columns.column("Customer Id", "id",
DataTypes.integerType()),
Columns.column("First Name", "first_name",
DataTypes.stringType()),
Columns.column("Last Name", "last_name",
DataTypes.stringType()),
Columns.column("Created
Date", "date", DataTypes.dateType()))
Columns.column("Customer Age", "age",
DataTypes.integerType()))
6 : Add a title text and page number to a report
.title(//title of the report
Components.text("SimpleReportExample")
.setHorizontalAlignment(HorizontalAlignment.CENTER))
.pageFooter(Components.pageXofY())//show page number on the page footer
7 : Finally set the datasource query and connection
.setDataSource("SELECT id, first_name, last_name, created_date,age FROM customers ", connection);
Now below the full example here.
SimpleReportTest.java
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;
public class DynamicSimpleTest {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root", "admin123");
} 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()),
Columns.column("First Name", "first_name", DataTypes.stringType()),
Columns.column("Last Name", "last_name", DataTypes.stringType()),
Columns.column("Created_date", "created_date", DataTypes.dateType()),
Columns.column("Customer Age","age",DataTypes.integerType()))
.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, created_date,age FROM customers",
connection);
try {
//show the report
report.show();
//export the report to a pdf file
report.toPdf(new FileOutputStream("d:/report1.pdf"));
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
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;
public class DynamicSimpleTest {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root", "admin123");
} 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()),
Columns.column("First Name", "first_name", DataTypes.stringType()),
Columns.column("Last Name", "last_name", DataTypes.stringType()),
Columns.column("Created_date", "created_date", DataTypes.dateType()),
Columns.column("Customer Age","age",DataTypes.integerType()))
.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, created_date,age FROM customers",
connection);
try {
//show the report
report.show();
//export the report to a pdf file
report.toPdf(new FileOutputStream("d:/report1.pdf"));
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
After Running It, It
generates a report in pdf as below.
No comments:
Post a Comment