2004 IR Help Posted August 22, 2006 Share Posted August 22, 2006 By: Sachin - murgundesachin To Embed File generated from iReport to java 2005-07-30 03:44 hi Friends I have Generated .jasper Reports thise are working fine,But now i want to call this reports from java class. I am very very new to Java. so it would be very nice if any one would help me with small code and steps to do. Regards Sachin By: John Zoetebier - johnzoet RE: To Embed File generated from iReport to java 2005-07-30 15:05 The following program QuoteProductReport.java loaded a compiled Jasper Report jasper file. The report is either show as preview or a print dialog pops up to print the report. The program shows a report that uses both parameters and a JDBC connection. The SQL is in master report and sub reports. Code:=> /* * TS Quote (http://www.transparent.co.nz) * Copyright (c) 2005 Transparent Systems Limited * Refer to terms in /doc/LICENSE.txt * */ /* * Created on Jan 14, 2004 * */ package tsquote.report; import java.io.File; import java.math.BigDecimal; import java.sql.SQLException; import java.text.DateFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.logging.Logger; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintServiceAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.PrintServiceAttributeSet; import javax.print.attribute.standard.MediaSizeName; import javax.print.attribute.standard.OrientationRequested; import javax.print.attribute.standard.PrinterName; import tsquote.controller.QueryHandler; import tsquote.exception.FinderException; import java.util.logging.Level; import javax.sql.DataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRPrintServiceExporter; import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter; import net.sf.jasperreports.view.JasperViewer; import tsquote.db.DataSourceHandler; import tsquote.util.Configuration; import tsquote.util.FileUtil; import tsquote.util.Parameter; /** * @author John Zoetebier * */ public class QuoteProductReport { private static Logger logger = Logger.getLogger("report"«»); private static QueryHandler queryHandler = QueryHandler.getInstance(); private static Map reportMap = new HashMap(); private static String jasperFileName; private static File jasperFile; private static boolean _printPreview; /** * */ private QuoteProductReport() { super(); } public static void print(Integer companyID, Integer customerID, Integer quoteID, boolean printPreview) { _printPreview = printPreview; // Check if jasper report is present jasperFileName = FileUtil.createFileName(Configuration.getProperty("home.dir", ""«») ,"report", "QuoteProductReport.jasper"«»); jasperFile = new File(jasperFileName); String msg = null; if (!jasperFile.exists()) { msg = "Cannot find jasper file: " + jasperFileName; logger.log(Level.SEVERE, msg); return; } // Set generic reportMap fields reportMap.put("message", ""«»); String dateFormatString = Parameter.getParameter("format.shortdate", "dd/MM/yyyy"«»); DateFormat dateFormat = new SimpleDateFormat(dateFormatString); reportMap.put("current_date", dateFormat.format(new Date())); reportMap.put("quote_id", quoteID); String reportPath = FileUtil.createFileName(Configuration.getProperty("home.dir", ""«»), "report"«»); reportPath += File.separator; reportMap.put("report_path", "/home/johnz/project/transparent/TSquote/report/"«»); /* * Get company data */ Map companyMap = null; StringBuilder companyDetails = new StringBuilder(); try { queryHandler.setTable("company"«»); queryHandler.setWhere("company_id=?", companyID); companyMap = queryHandler.getMap(); reportMap.put("company_name", companyMap.get("company_name"«»)); } catch (FinderException fE) { printReport("No company record found", fE.getMessage()); return; } try { queryHandler.setTable("company_address"«»); queryHandler.joinTable("address_type", "address_type_id", "company_address"«»); queryHandler.joinTable("suburb", "suburb_id", "company_address"«»); queryHandler.joinTable("city", "city_id", "company_address"«»); queryHandler.joinTable("country", "country_id", "company_address"«»); queryHandler.setWhere("company_id=?", companyID); queryHandler.andWhere("address_type_code='WORK'"«»); Map addressMap = queryHandler.getMap(); appendStringBuilder(companyDetails, addressMap.get("company_address.address1"«»)); appendStringBuilder(companyDetails, addressMap.get("company_address.address2"«»)); appendStringBuilder(companyDetails, addressMap.get("suburb.suburb"«»)); appendStringBuilder(companyDetails, addressMap.get("city.city"«»)); appendStringBuilder(companyDetails, addressMap.get("country.country"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No company WORK address found: " + fE.getMessage())); } try { queryHandler.setTable("company_contact"«»); queryHandler.joinTable("contact_type", "contact_type_id", "company_contact"«»); queryHandler.setWhere("company_id=?", companyID); queryHandler.andWhere("contact_type_code='PHONE/WORK'"«»); Map contactMap = queryHandler.getMap(); appendStringBuilder(companyDetails, "Phone : " + contactMap.get("company_contact.contact"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No company PHONE/WORK: " + fE.getMessage())); } try { queryHandler.setTable("company_contact"«»); queryHandler.joinTable("contact_type", "contact_type_id", "company_contact"«»); queryHandler.setWhere("company_id=?", companyID); queryHandler.andWhere("contact_type_code='EMAIL/WORK'"«»); Map contactMap = queryHandler.getMap(); appendStringBuilder(companyDetails, "Email : " + contactMap.get("company_contact.contact"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No company EMAIL/WORK: " + fE.getMessage())); return; } reportMap.put("company_details", companyDetails.toString().trim()); /* * Get customer data */ Map customerMap = null; StringBuilder customerDetails = new StringBuilder(); try { queryHandler.setTable("customer"«»); queryHandler.joinTable("title", "title_id"«»); queryHandler.setWhere("customer_id=?", customerID); customerMap = queryHandler.getMap(); String customerName = new String(); if (customerMap.get("title.title"«») != null) { customerName = customerMap.get("title.title"«») + " "; } customerName = customerName + customerMap.get("customer.first_name"«») + " " + customerMap.get("customer.last_name"«»); reportMap.put("customer_name", customerName); } catch (FinderException fE) { printReport("No customer record found", fE.getMessage()); return; } try { queryHandler.setTable("address"«»); queryHandler.joinTable("address_type", "address_type_id", "address"«»); queryHandler.joinTable("suburb", "suburb_id", "address"«»); queryHandler.joinTable("city", "city_id", "address"«»); queryHandler.joinTable("country", "country_id", "address"«»); queryHandler.setWhere("customer_id=?", customerID); queryHandler.andWhere("address_type_code='LIVING'"«»); Map addressMap = queryHandler.getMap(); appendStringBuilder(customerDetails, addressMap.get("address.address1"«»)); appendStringBuilder(customerDetails, addressMap.get("address.address2"«»)); appendStringBuilder(customerDetails, addressMap.get("suburb.suburb"«»)); appendStringBuilder(customerDetails, addressMap.get("city.city"«»)); appendStringBuilder(customerDetails, addressMap.get("country.country"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No customer LIVING address found: " + fE.getMessage())); } try { queryHandler.setTable("contact"«»); queryHandler.joinTable("contact_type", "contact_type_id", "contact"«»); queryHandler.setWhere("customer_id=?", customerID); queryHandler.andWhere("contact_type_code='PHONE/HOME'"«»); Map contactMap = queryHandler.getMap(); appendStringBuilder(customerDetails, "Phone : " + contactMap.get("contact.contact"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No customer PHONE/HOME: " + fE.getMessage())); } try { queryHandler.setTable("contact"«»); queryHandler.joinTable("contact_type", "contact_type_id", "contact"«»); queryHandler.setWhere("customer_id=?", customerID); queryHandler.andWhere("contact_type_code='EMAIL/HOME'"«»); Map contactMap = queryHandler.getMap(); appendStringBuilder(customerDetails, "Email : " + contactMap.get("contact.contact"«»)); } catch (FinderException fE) { logger.log(Level.INFO, ("No customer EMAIL/HOME: " + fE.getMessage())); } reportMap.put("customer_details", customerDetails.toString().trim()); printReport(); } /** * Print report */ private static void printReport(String... message) { if (message.length > 0) { logger.log(Level.SEVERE, message[0] + "n" + message[1]); reportMap.put("message", message[0] + " Please check log"«»); } try { DataSource dataSource = DataSourceHandler.getDataSource(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFileName, reportMap, dataSource.getConnection()); // For print attributes see API docs: j2sdk-1_4_2/docs/api/index.html PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(OrientationRequested.PORTRAIT); printRequestAttributeSet.add(MediaSizeName.ISO_A4); PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(); // Linux: print queues like "draft", "normal", "photo", etc // Windows: use physical printername like "Epson Stylus COLOR 680" or "HP LaserJet 5000 Series PCL" String printerNameTemp = Configuration.getDecodedProperty("report.printer", ""«»); if (!printerNameTemp.equals(""«»)) { PrinterName printerName = new PrinterName(printerNameTemp, Locale.getDefault()); printServiceAttributeSet.add(printerName); } JRPrintServiceExporter exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter( JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); exporter.setParameter( JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet); exporter.setParameter( JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter( JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); if (_printPreview) { // preview is started in non_modal JasperViewer.viewReport(jasperPrint, false); } else { exporter.exportReport(); } } catch (JRException jrE) { String msg = "nError printing report:nPlease check printer parameter.n" + jrE.getMessage(); logger.log(Level.SEVERE, msg); return; } catch (SQLException sqlE) { String msg = "nError printing report:nPlease check database connection.n" + sqlE.getMessage(); logger.log(Level.SEVERE, msg); return; } } private static void appendStringBuilder(StringBuilder builder, Object field) { if (field == null) return; String text = (String) field; if (text.length() == 00) return; builder.append(text); builder.append('n'); } } => By: Sachin - murgundesachin RE: To Embed File generated from iReport to java 2005-07-31 02:01 thanks for ur reply iam working over it.i will get to u within some time thanks right now iam using this code but it is giving me Exception iam working over it Code:[code]*/ import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import net.sf.jasperreports.*; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.xml.JRXmlLoader; import net.sf.jasperreports.view.JasperViewer; import java.util.*; public class Rep { Rep() { Connection con; Statement stmt; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"«»); con = DriverManager.getConnection("jdbc:odbc:FleetManagment" ); stmt = con.createStatement() ; JasperDesign jasperDesign = JRXmlLoader.load("E:\FleetManagment(Local)\Reports\Invoice\rpt_Invoice.xml"«»); JasperReport jasperReport = JasperCompileManager.compileReport("c:\rpt_Invoice.jasper"«»); Map parameters = new HashMap(); parameters.put("Title", "Invoice Information"«»); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, con); JasperExportManager.exportReportToPdfFile(jasperPrint, "c:\rpt_Invoice.pdf"«»); JasperViewer.viewReport(jasperPrint); }catch(Exception e) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { try { Rep objReport=new Rep(); }catch(Exception e){ } } }Post edited by: tcloonan, at: 2006/08/21 21:23 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now