| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||
I often see the question in the JDEV forums “How can I access Jasper Reports in Jdeveloper?”. Jasper reports is a great tool for reporting, it’s written in 100% java and its free to use. If you dont want to be burdened with handcoding your reports, I sugguest using IReport, which like Jasper reports its written in java and free.
Below is a sample of code that I use in my application to call a jasper report and create a PDF or RTF file for output to the browser. The parts in Bold are the actual parts needed to do the reporting. The rest of the code is actually a system I setup to be able to have dynamic reporting with the user selecting the parameters from views.
public void PrintReport()
{
//load the parameters
ReportViewImpl myReportView = getReportView();
PatchViewImpl myPatchView = getPatchView1();
RequestViewImpl myRequestView = getRequestView();PatchViewRowImpl currentPatchRow = (PatchViewRowImpl)myPatchView.getCurrentRow();
RequestViewRowImpl currentRequestRow = (RequestViewRowImpl)myRequestView.getCurrentRow();
Row currentReportRow = myReportView.getCurrentRow();String ReportName = (String) currentReportRow.getAttribute(”Filename”);
String ReportPara = (String) currentReportRow.getAttribute(”ParameterName”);
String parameter = “”;if (ReportPara.compareTo(”PATCH_ID”)==0) parameter = currentPatchRow.getPatchId().getSequenceNumber().toString();
if (ReportPara.compareTo(”REQUEST_ID”)==0) parameter = currentRequestRow.getRequestId().getSequenceNumber().toString();
if (ReportPara.compareTo(”NONE”)==0) parameter = “0″;
//if (ReportPara.compareTo(”TICKET_ID”)==0) parameter = currentTicketRow.getTicketId().getSequenceNumber().toString();Map parameters = new HashMap();
parameters.put(ReportPara, new Integer(parameter));
try
{
String UserName = getApplicationUserName();
File inFileName = new File (”applications/reports/”+ReportName+”.jasper”);
File outPutPDF = new File (”applications/reports/output/”+UserName+”_temp.pdf”);
File outPutRTF = new File (”applications/reports/output/”+UserName+”_temp.rtf”);JasperPrint jasperPrint = JasperFillManager.fillReport(inFileName.getPath(), parameters, getCurrentConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint, outPutPDF.getPath());
}
catch (JRException e) {e.printStackTrace();}
catch (Exception e) {e.printStackTrace();}
}
As you can see there is not much work to do in order to call a report. One of the problems that I had, was determining where to put my report files, using OC4J 10.2.1. I created a directory under the j2eeapplications directory on my server called reports. This allows me to copy my files out there without having to use a deployment file, or rebooting the server, it also allows my testing and production boxes to access the same reports.
The next step is calling the fill manager for Jasper, this takes your report and populates it with data. There are many different ways to pass the fill manager a connection to the database, I use a routine that I found on the JDEV forums for accessing the current connection in my Application Module. The getCurrentConnection() is a function in my base application module class:
public Connection getCurrentConnection() {
Statement st = null;
try {
st = getDBTransaction().createStatement(0);
return st.getConnection();
}
catch (SQLException s) {
s.printStackTrace();
return null;
}
finally {
if (st != null) try { st.close(); } catch (SQLException s2) {}
}
}
Using this function I am able to use the same database connection as my application module. After the report is filled you call the Jasper Export routine that matches the output that you want to create.
This is not the only way to work a jasper report into your application, I have also seen it done coding it in the view layer of an MVC application. I prefer to place my routines that access data into my Application Modules. This way if I need to change my front end, I do not have to rewrite as much code. (Which I am currently doing converting my application from UIX to JSF).
Please post your comments on this article good or bad.
Till next time…….
Kelly Burton
August 20, 2008 at 3:21 am
thank you for the share,
i have a problem with my report, i use jasper report to generate my report but i want to call that from my swing application. how to do that? thank’s for the answer
August 20, 2008 at 10:32 am
The method would be the same as here for generating the report. However for showing it inside your swing application it would be different. You would have to find an object that you can stream the completed report to for display. In my web based Application I am streaming the report to an empty page.
September 8, 2008 at 6:42 am
Hi!
I’m trying to call a report from my app. I have created the report with ireports and when execute from ireport every thing work allright.
But when I call the report from my adf application i get the next error:
Could not create the report : XML-20147: (Error) Elemento no válido ‘topPen’ en el contenido de ‘box’, se esperaba una etiqueta de cierre
It seems that don´t accept the topPen tag.
Do you have any idea what can be the prooblem?
Thanks for helping us!
Rowan
May 12, 2009 at 7:54 am
http://jasperforge.org/plugins/wpress/2009/05/12/jaspersoft-projects-are-moving-to-open-source-v3-licenses/