Wednesday 13 November 2013
Sorting ArrayList with custom fields by Id, Name , City , Age using Comparator Interface in java
Thursday 7 November 2013
How to create a report using Jasper Report in java
//-------------------- Save as ReportGenerator.java ----------------------------
package com.ramsis;
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","ABCD");
Class.forName("com.mysql.jdbc.Driver");
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","public");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gts","root","public");
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/EmployeeReport4.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();
}
}
//----------------------- Save as EmployeeReport.jrxml ------------------------
<?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_aman.kumar">
<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 - By Java]]></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="100" 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="100" y="0" width="69" height="24" />
<textFieldExpression>
<![CDATA[$F{Last_Name}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Out Put:
//------------------------- Save as HelloJasper.java ---------------------
package com.ramsis;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JREmptyDataSource;
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;
import net.sf.jasperreports.view.JasperViewer;
public class HelloJasper {
public static void main(String args[]){
String reportSource = "jrxml/HelloReportWorld.jrxml";
String reportDest = "jrxml/HelloReportWorld.html";
Map <String, Object> params = new HashMap <String, Object>();
//params.put("reportTitle", "Hello Report World");
//params.put("author", "Craig Conover");
//params.put("startDate", (new java.util.Date()).toString());
try
{
JasperReport jasperReport =
JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
JasperExportManager.exportReportToHtmlFile(
jasperPrint, reportDest);
JasperViewer.viewReport(jasperPrint);
}
catch (JRException ex)
{
ex.printStackTrace();
}
}
}
//-------------------------- Save as HelloReportWorld.jrxml ------------------
<jasperReport name="HelloReportWorld">
<detail>
<band height="200">
<staticText>
<reportElement mode="Opaque" x="50" y="10" width="500" height="30" />
<text><![CDATA[ ID Name Phone City ]]></text>
</staticText>
<staticText>
<reportElement x="50" y="20" width="500" height="30" />
<text><![CDATA[101 Aman Kumar 997152663 Noida ]]></text>
</staticText>
<staticText>
<reportElement x="50" y="30" width="500" height="30" />
<text><![CDATA[102 Suraj Kumar 929300440 New Delhi ]]></text>
</staticText>
<staticText>
<reportElement x="50" y="40" width="500" height="30" />
<text><![CDATA[102 Pavan Yadav 9293344550 Noida ]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Out Put:
Monday 4 November 2013
How to canvert java object into xml file (Vice Versa)
No extra jaxb libraries are required if you are using JDK1.6
For object that need to convert to / from XML file, it have to annotate with JAXB annotation
//---------------- Save as Customer.java ------------------------
package com.ramsiscode.jaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
int id;
String name;
int age;
String address;
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
@XmlElement
public void setAddress(String address) {
this.address = address;
}
}
//-----------------------------------------------------------
JAXB marshalling example, for convert customer object into a XML file
//------------------- save as ConvertXML.java -------------------------
package com.ramsiscode.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ConvertXML {
public static void main(String args[]){
Customer customer = new Customer();
customer.setId(101);
customer.setName("Aman");
customer.setAge(28);
customer.setAddress("Noida, Uttar Pradesh India.");
try{
File file = new File("D:\\customer.XML");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
//----------------- output -----------------------------------
//------------------------------------------------------------------------------
JAXB unmarshalling example, convert a XML file content into a customer object
//------------------------- save as CustomerXMLRead.java ------------------------
package com.ramsiscode.jaxb;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class CustomerXMLRead {
public static void main(String args[]){
try {
File file = new File("D:\\customer.XML");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.getName());
System.out.println(customer.getId());
System.out.println(customer.getAge());
System.out.println(customer.getAddress());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}