Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.util.HashMap;
  5.  
  6. import net.sf.jasperreports.engine.JRException;
  7. import net.sf.jasperreports.engine.JRExporterParameter;
  8. import net.sf.jasperreports.engine.JasperCompileManager;
  9. import net.sf.jasperreports.engine.JasperExportManager;
  10. import net.sf.jasperreports.engine.JasperFillManager;
  11. import net.sf.jasperreports.engine.JasperPrint;
  12. import net.sf.jasperreports.engine.JasperReport;
  13. import net.sf.jasperreports.engine.export.JRXlsExporter;
  14.  
  15.  
  16. public class ReportClass {
  17.  
  18.     public static Connection establishConnection()
  19.     {
  20.     Connection connection = null;
  21.     try
  22.     {
  23.     Class.forName("oracle.jdbc.driver.OracleDriver");
  24.     String oracleURL = "jdbc:oracle:thin:@localhost:1521:mySID";
  25.     connection = DriverManager.getConnection(oracleURL,"username","password");
  26.     connection.setAutoCommit(false);
  27.     }
  28.     catch(SQLException exception)
  29.     {
  30.     exception.printStackTrace();
  31.     }
  32.     return connection;
  33.  
  34.     }
  35.    
  36.     /* JasperReport is the object
  37.     that holds our compiled jrxml file */
  38.     JasperReport jasperReport;
  39.  
  40.  
  41.     /* JasperPrint is the object contains
  42.     report after result filling process */
  43.     JasperPrint jasperPrint;
  44.    
  45.     // connection is the data source we used to fetch the data from
  46.     Connection connection = establishConnection();
  47.     // jasperParameter is a Hashmap contains the parameters
  48.     // passed from application to the jrxml layout
  49.     HashMap jasperParameter = new HashMap();
  50.  
  51.     // jrxml compiling process
  52.     jasperReport = JasperCompileManager.compileReport("C://sample_report.jrxml");
  53.  
  54.     // filling report with data from data source
  55.  
  56.     jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);
  57.    
  58.     // exporting process
  59.     // 1- export to PDF
  60.     JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report.pdf");
  61.  
  62.     // 2- export to HTML
  63.     JasperExportManager.exportReportToHtmlFile(jasperPrint, "C://sample_report.html" );
  64.  
  65.     // 3- export to Excel sheet
  66.     JRXlsExporter exporter = new JRXlsExporter();
  67.     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  68.     exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://simple_report.xls" );
  69.  
  70.     exporter.exportReport();   
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement