This is link of original post : LINK
1) Structure of Employee Report example
2) Add below list of jar files in EmployeeReport example
commons-beanutils-1.7.jar
commons-collections-3.1.jar
commons-digester-1.8.jar
commons-lang-2.1.jar
commons-logging-1.1.jar
iText-1.3.jar
jasperreports-1.0.3.jar
ojdbc14.jar
3) create table like this below
create table Employee(
First_Name VARCHAR2(10),
Last_Name VARCHAR2(10)
);
insert into Employee values('satyam','reddy');
insert into Employee values('pallavi','reddy');
4) create a jrxml folder in your EmployeeReport Project
In side jrxml folder create EmployeeReport.jrxml file and paste below code in inside that file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="EmployeeReport_Satyamsoft">
<queryString>
<![CDATA[select First_Name, Last_Name from employee e]]>
</queryString>
<field name="First_Name" />
<field name="Last_Name" />
<title>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="180" height="15"/>
<textElement/>
<text><![CDATA[Jasper Report - satyamsoft]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="69" height="24" />
<textElement verticalAlignment="Bottom" />
<text>
<![CDATA[First Name: ]]>
</text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="79" height="24" />
<text>
<![CDATA[Last Name: ]]>
</text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="30">
<textField>
<reportElement x="0" y="0" width="69" height="24" />
<textFieldExpression>
<![CDATA[$F{First_Name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="140" y="0" width="69" height="24" />
<textFieldExpression>
<![CDATA[$F{Last_Name}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
5) create ReportGenerator class and paste inside this class
package com.satyamsoft;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
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;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class ReportGenerator {
Connection conn;
public void generateReport() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","123456");
System.out.println("Loading Report Designs");
InputStream input = new FileInputStream(new File("jrxml/EmployeeReport.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("ReportTitle", "PDF JasperReport");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);
//Exporting the report
OutputStream output = new FileOutputStream(new File("report/EmployeeReport.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
System.out.println("Report Generation Complete");
conn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ReportGenerator().generateReport();}
}
6) After executing the class we will get EmployeeReport.pdf file in our report folder then the pdf file looks like below
No comments:
Post a Comment