Solved

How Do I Pass a Value from a Scriplet Variable to an iReport Parameter?

Posted on 2010-11-12
8
2,423 Views
Last Modified: 2012-05-10
I wrote a scriptlet that executes an Oracle SQL function called "get_bps_rept_gn". It returns a number that I am trying to pass to an iReport 3.5.1 parameter. The parameter is called $P{bps_rept_gn}. Whenever I try to pass the value to this parameter, the parameter displays as null. I can successfully pass the value to a variable of the same name as the parameter ($V{bps_rept_gn}. This value displays in the report. I'm new to scriplets so I may be missing something in the syntax.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.BPS_scriptlet_pg;

import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
/*import net.sf.jasperreports.engine.query.JRQueryExecuter;*/
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;
import java.util.HashMap;
import java.util.Map;


/**
 *
 * @author 
 */
public class BPS022_scriptlet extends JRDefaultScriptlet {
    
    Connection con;



@Override
    public void afterReportInit() throws JRScriptletException
    {
        con = (Connection)this.getParameterValue("REPORT_CONNECTION");

        try{
        seeCon(con); 


Integer pi_rept_gn2 = 0;

CallableStatement cs4 = con.prepareCall("{? = call get_bps_rept_gn}");

cs4.registerOutParameter(1, java.sql.Types.NUMERIC);

cs4.executeUpdate();

pi_rept_gn2 = cs4.getInt(1);

this.setVariableValue("bps_rept_gn", pi_rept_gn2);

//parametersMap.put("bps_rept_gn", pi_rept_gn2.intValue());
Map parameters = new HashMap();
		parameters.put("bps_rept_gn", pi_rept_gn2);



        } catch (SQLException e){
            System.out.println("Caught SQL Exception: " + e.getErrorCode());
        }


        System.out.flush();
    }
  
    public void seeCon (Connection c) throws SQLException
    {
        System.out.println("Con is closed? :" + c.isClosed());
    }


}

Open in new window

0
Comment
Question by:shellybelly999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 34128157
I see where you put the new value into a new HashMap of parameters:
Map parameters = new HashMap();
            parameters.put("bps_rept_gn", pi_rept_gn2);

but I don't see where that is put somewhere that would be visible outside of this method.  And this method doesn't call the report, so it can't help you there.  You create the object in the method, then the method ends and the object goes away.

Where do you want the new parameter value to be used to be sent to iReport?
0
 

Author Comment

by:shellybelly999
ID: 34133725
I've linked the scriptlet to the report so that's why I'm not calling the report. There is a parameter defined in the report that's called $P{bps_rept_gn}. I was hoping that the new parameter value would replace the default value of the parameter. But maybe this is not possible. Maybe the parameter value can only be passed at the time that the report is called rather than after the report is already initialized?
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 34136257
I don't know if you can change a value for the report with the this.setVariableValue call in this scriptlet -- I don't see where the variable is defined, or the report.

But I do know that creating the HashMap will have no effect outside this scriptlet.  So if you need the hashmap, you have to do it differently.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:shellybelly999
ID: 34142050
I'm sorry. I didn't include the .jrxml report file with the code because that's a separate file. The scriptlet was being called from the .jrxml file. After investigating, I found that I need to call the report in a manner similar to this:

jasperReport = JasperCompileManager.compileReport(
          "reports/jasperreports_demo.jrxml");
      jasperPrint = JasperFillManager.fillReport(
          jasperReport, new HashMap(), new JREmptyDataSource());
      JasperExportManager.exportReportToPdfFile(
          jasperPrint, "reports/simple_report.pdf");

So your answer got me on the right track and I'm not trying to pass the parameter from the scriplet called by the report. Now I'm trying to create a little Java app that will call the report and pass the parameter to it. Thanks for your help and please wish me luck!
0
 

Author Closing Comment

by:shellybelly999
ID: 34142059
I didn't supply all the info that Mr. Coffee needed to give a complete solution. But I'm happy because he straightened out my incorrect approach to the problem.
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 34142263
Thanks, and good luck!

Also -- it's great that you posted your final solution.  Was it the this.setVariableValue for the named variable that set the value the way you wanted?  As long as you had the call done correctly, as you've posted?
0
 

Author Comment

by:shellybelly999
ID: 34160098
I successfully passed the parameters in my test! In order to pass the parameter, I first created a parameter called $P{ReportTitle} in my iReport.

Then I created a Java program to run the report. Before the statement to fill the report, I created the statements below which change the value in the $P{ReportTitle} parameter to "Successfully Changed Title":

//Preparing parameters
Map parameters = new HashMap();
parameters.put("ReportTitle", "Successfully Changed Title");

Then I filled the report with this code:

JasperFillManager.fillReportToFile(reportDirectory + "\\" + reportName + ".jasper",
               parameters,connection);  // creates .jrprint file

Now back to my project!
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 34161107
Great -- thanks for posting!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question