0

How to pass parameters to query in JRXML file of jasper reports from JAVA in Jasper soft studio (not in iReport)?

1) My java class is as follows

public class PdfFromXmlFile {     
    public static void main(String[] args) throws JRException, IOException, ClassNotFoundException, SQLException {

    try {
         //Object aaa="600";
         // Compile jrxml file.
        HashMap<String,Object> map = new HashMap<String,Object>();   
        map.put("my_ciperf",400);

        jasperPrint = JasperFillManager.fillReport(jasperReport,map, conn);
        // ExporterOutput
        OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(           "D:\\JasperReports\\StyledTextReport3.pdf");
        // Output
    } 
}

2) Now following is my JRXML syntax looks like

 <parameter name="my_ciperf" class="java.lang.Integer"></parameter>
 <queryString>
    <![CDATA[SELECT * FROM CiPerfDev_TestData WHERE CiPerfDev_TestData.CapacityUsers = $P{my_ciperf}]]
 </queryString>

NOTE: In the above program I want to pass "my_ciperf" parameter to my JRXML

Output: I am getting report with no data in it.

Observation: When ever I am hardcoding my_ciperf value directly into query then report is getting generated.

Question: How to pass parameters from java to query in JRXML?

Matt
  • 14,906
  • 27
  • 99
  • 149
  • You are doing it correctly, what is the field type of CapacityUsers? – Petter Friberg May 03 '16 at 07:46
  • Hi @Petter Friberg -> CapacityUsers field type is int . Is it possible to pass parammeters to a query in JRXML? Because following expression is can able display the value of my_ciperf in the report <![CDATA[$P{my_ciperf}]]> – ramakrishna May 03 '16 at 10:03
  • Yeah its possibile and you are doing it the standard way (correct way) using prepared statement, see Alex K's answer [here](http://stackoverflow.com/questions/11871042/jasperreports-passing-parameters-to-query) for the 2 different ways – Petter Friberg May 03 '16 at 10:09
  • I could not understand if you see the $P{my_ciperf} value in textField or not? There is something strange going on are you using subdataset (hence not using the main dataset query) – Petter Friberg May 03 '16 at 10:11
  • @Gangaraju if its not specifically about the IDE (iReport or JasperSoft studio), do not add these tags, in this case it seems to be about studio but it really is not – Petter Friberg May 03 '16 at 10:20
  • @PetterFriberg hi i am still unable to figure out the solution. if you have implemented any code before like this, please share your code to me at varmasdatla@gmail.com – ramakrishna May 04 '16 at 05:24
  • Your method is correct try to use $P!{my_ciperf} (even if $P{} is better), and but the $P{my_ciperf} in a textField to see that it contains data example in title band. – Petter Friberg May 04 '16 at 07:46

0 Answers0