Thursday, October 29, 2009

Using Jasper Reports In ADF Applications

You can check this for step by step calling jasper report

http://sameh-nassar.blogspot.ie/2012/12/using-jasper-report-in-adf-application.html


One of the solutions for generating reports when you use JDeveloper is using Jasper Report.
Jasper report is some of libraries (.Jar files) and .xml or .jrxml file that represent your report.
One of the easy ways to write .xml file is using IReport program. IReport is a program that manage you to design your report graphically then save your report to xml or jrxml file. From JDeveloper after you add jasper reports libraries to your project you can read your report (.xml file) and print it as PDF file or Excel File or Jasper Viewer.
Jasper Report Libraries is:
1-commons-digester-1.7.jar
2-itext-1.4.5.jar
3-jasperreports-3.0.0.jar
4-poi-2.5.1-final-20040804.jar
5- jasper-compiler-jdt-5.5.15.jar

You Can Download IReport From :
To configure your application to use Jasper Reports:
1- Add jasper report libraries to your project.
2- In web.xml add this
< resource-ref>
< res-ref-name> jdbc/OracleDBConnectionDS < /res-ref-name>
< res-type> javax.sql.DataSource < /res-type>
< res-auth> Container < /res-auth>
< /resource-ref>
3- From backingbean write this method:
----------- Imports ----------------
import java.io.*;
import java.util.*;
import java.sql.Connection;import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;import javax.naming.NamingException;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
----------------------------------------
public void printReport() throws FileNotFoundException, JRException, NamingException, SQLException, IOException {
InputStream input = new FileInputStream(new File("c:/yourJasperReport.xml"));
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
Map parameters = new HashMap();
// parameters.put("jasperParamenterName", "ParameterValue"); Used if you want to pass a parameter to a jasper report
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/OracleDBConnectionDS"); // get from your application module configuration
Connection conn = ds.getConnection();
JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
///// For printing report as PDF file ////////
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(print,byteArrayOutputStream);
out.write(byteArrayOutputStream.toByteArray());
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
//////// For printing report as excel file //////
OutputStream ouputStream = new FileOutputStream(new File("c:/yourReport.xls")); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
exporterXLS.exportReport();
ouputStream.write(byteArrayOutputStream.toByteArray());
ouputStream.flush();
ouputStream.close();
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:/yourReport.xls");
///// for printing report as a jasper report viewer /////
JasperViewer.viewReport(print, true);
}
For More Information About Using Jasper Report :

29 comments:

  1. Hi,

    How Can i do parameter page to pass to jasper?

    Similar to Oracle forms unbound items / Reports Integration.

    ReplyDelete
  2. Hi Bernat,

    You can make a parameter page and bind its fields in backingbean and set report parameter with the entered data as:

    in parameter page put af:inputText and set its value with variable in backingbean (assume the variable is "param1")

    in Jasper report bean set parameter as

    Map parameters = new HashMap();
    parameters.put("jasperParamenterName", "Param1");

    ReplyDelete
  3. but your code work only in server side , can i use some thing to view report in client side ,
    really it is urgent

    ReplyDelete
  4. Hi, i have my report an run ok but when execute JasperViewer.viewReport(print, true); my weblogic server (integrated with jdeveloper) is shutdown. Can you help me? I have j developer 11g release 1.

    Thanks

    ReplyDelete
  5. use JasperViewer.viewReport(print, false);

    ReplyDelete
  6. Hi, I m getting this error after running report, "XML-20149: (Error) Element 'jasperReport' used but not declared".

    How to correct this?

    ReplyDelete
  7. I think you not follow my steps correctly because I didn't write "jasperReport" so, check your code and if the problem not solved send me you code in my email samehnassar39@gmail.com.

    ReplyDelete
  8. Hi, I actually get the same error..
    Parse Error at line 2 column 14: : XML-20149: (Error) Element 'jasperReport' used but not declared.
    org.xml.sax.SAXParseException: : XML-20149: (Error) Element 'jasperReport' used but not declared.
    at oracle.xml.parser.v2.XMLError.flushErrorHandler(XMLError.java:422)
    at oracle.xml.parser.v2.XMLError.flushErrors1(XMLError.java:287)
    at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:343)
    at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:226)
    at weblogic.xml.jaxp.RegistryXMLReader.parse(RegistryXMLReader.java:173)
    at org.apache.commons.digester.Digester.parse(Digester.java:1745)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:238)
    .
    .
    .
    .
    .

    this happened when executing line
    JasperDesign design = JRXmlLoader.load(input);

    tools that i use are :
    Jdeveloper 11.1.1.4.0 and JasperSoft iReport Designer 4.0.2

    regards,

    ReplyDelete
  9. Hi,
    check the version of jasper library because every version of IReport should use a specific version of jasper report. Try to use IReport version 3.0.

    Regards,

    ReplyDelete
  10. Thanks Sameh for This nice topic
    its very usefull for me

    Thanks again for share

    ReplyDelete
  11. Thanks Sameh for the nice and useful post

    ReplyDelete
  12. HI Samesh,

    we are entity object based on transient attributes and view object based on this entity now we want to pass data in the view object to generate jasper report so instead of using ds can we use map.put(".jrxml parameter 1"," value of view attribute1")

    ReplyDelete
    Replies
    1. I already use map in my code you will find this line Map parameters = new HashMap();
      which hold all your report parameters

      Delete
  13. i had try the code and it success.
    but when i closed the pdf file or jasper view report, the weblogic force to shutdown.
    i used jdev adf 11.1.1.5.0.anybody knows
    TQ

    ReplyDelete
    Replies
    1. Hi,

      try to write JasperViewer.viewReport(print, false);

      instead of JasperViewer.viewReport(print, true);

      Delete
  14. Hi Sameh,

    your code work only in server side , can i use some thing to view report in client side

    ReplyDelete
    Replies
    1. I will make a new post for jasper, I will explain it in more detail.

      Delete
    2. Check this

      http://sameh-nassar.blogspot.com/2012/12/using-jasper-report-in-adf-application.html

      Delete
  15. Hi Sameh,

    Could you please tell me the steps after downloading all these package.
    i'm new in Jdev 11g but i can understand clear steps so ...

    ReplyDelete
    Replies
    1. I will make a new post for jasper, I will explain it in more detail. Just follow my blog this week.

      Regards

      Delete
    2. Check this http://sameh-nassar.blogspot.com/2012/12/using-jasper-report-in-adf-application.html

      Delete
  16. جزاك الله خيرا

    ReplyDelete
  17. tahnk sameh

    which better using oracle reports with xml publisher or using jasper

    ReplyDelete
    Replies
    1. - XML Publisher is part of Oracle BI you should setup OBI environment and customer will pay licence for it but it has a good features like generated custom reports.

      - Jasper is xml file no need for report server to run, it will be included with in the application war file and it is free (no licence required) but it is static report.

      So it depending on the customer requirements and how the customer is big, some customers can pay licence for reporting and other no.

      Delete
  18. i have used same procedure but run report button do nothing without any error

    ReplyDelete
    Replies
    1. Be sure button autoSubmit property set to "false"

      Delete
  19. PartialSubmit?? there is no autosubmit option in button property

    ReplyDelete