Code:
// Using a Database [JDBC connection]
import java.sql.Connection;
import java.io.FileNotFoundException;
import java.sql.DriverManager;
import java.util.HashMap;
import net.sf.jasperreports.engine.*;
public class JasperJDBCConnection {
public static void main(String[] args) throws FileNotFoundException, JRException {
JasperReport jasperReport = null;
String path = "D:/JasperTemplates/";
JasperPrint jasperPrint = null;
//Gettign the connection object
Connection conn = getConnection();
//Provide path for your JRXML template.
String templateName = path + "ReportSQL.jrxml";
//Provide path for your final pdf file.
String destinationFile = path + "ReportSQL.pdf";
//Compiling the template.
jasperReport = JasperCompileManager.compileReport(templateName);
//Sending a parameter with the logged in user name as value
HashMap parameters = new HashMap();
// Filling the report template with data
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
//Sending a Complete print of the report.
JasperPrintManager.printReport(jasperPrint, true);
//Exporting it to an PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);
}
//Getting the JDBC Connection object to be used in the template.The data base used for the sample is MYSQL running on localhost at port 3306
public static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "testjasper";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected to the database");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
Brief explanaition of the code.
1. ReportSQL.jrxml is the name of the JRXML template.
2. ReportSQL.pdf is the output pdf file.
3. getConnection() is a utility method used to get the connection object that is later used for retrieving data from the database. “testjasper” refers to the MYSQL data base running on the localhost port 3306.
4. JasperCompileManager.compileReport() takes in the template file and converts it into a jasper object. This is later used by the reporting engine to create the report.
5. JasperFillManager.fillReport() uses the compiled template [jasper object] ,hashmap[a dummy: ideally used to pass parameters],and the connection object created to Fill the report. The Connection object is used by the Jasper Reporting engine to fire the Report Query and get the data that is required to be filled in the report.
6. Finally the Report is exported to a PDF file using the JasperExportManager.exportReportToPdfFile() method
//XML file data source
Lets now focus on using XML flat file as a data source instead of using a fully fledged data base. This feature allows jasper Report to integrate with non java applications which provide the xml data that needs to be reported.
In this sample we will be using the same template we created earlier [with slight modifications] and use a static xml file as a data source.
code
import java.io.FileNotFoundException;
import java.util.HashMap;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.view.JasperViewer;
public class XMLDataSourceSample {
public static void main(String[] args) throws FileNotFoundException, JRException {
JasperReport jasperReport = null;
String recordPath = "/employee/person";
String xmlFileName = "Address.xml";
String path = "D:/JasperTemplates/";
JasperPrint jasperPrint = null;
//Provide path for your JRXML template.
String templateName = path+"ReportXML.jrxml";
//Provide path for your final pdf file.
String destinationFile = path+"ReportXML.pdf";
//Compiling the template.
jasperReport = JasperCompileManager.compileReport(templateName);
//Sending a parameter with the logged in user name as value
HashMap parameters = new HashMap();
//Creating the datasource
JRXmlDataSource jrxmlds = new JRXmlDataSource(path+xmlFileName, recordPath);
// Filling the report template with data
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,jrxmlds);
JasperViewer.viewReport(jasperPrint);
//Exporting it to an PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);
}
}
Brief explanation of the code.
1. ReportXML.jrxml is the name of the JRXML template.
2. ReportXML.pdf is the output pdf file.
3. JRXmlDataSource takes in two arguments Path of the xml file with data and the recordPath
4. JasperCompileManager.compileReport() takes in the template file and converts it into a jaspe object. This is later used by the reporting engine to create the report.
5. JasperFillManager.fillReport() uses the compiled template [jasper object] ,hashmap[a dummy: ideally used to pass parameters],and the JRXmlDataSource object created from the XML file
6. Finally the Report is exported to a PDF file using the JasperExportManager.exportReportToPdfFile() meathod
Address.xml
No comments:
Post a Comment