passing empty field as parameter in jasper ireport

一世执手 提交于 2019-12-24 11:57:51

问题


The query

select * from db_accessadmin.customerSummary 
where 
(accountNumber = $P{accountNo} or $P{accountNo}='')
and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
and (cast(requestDate as date) between (cast($P{fromDate} as date)) and (cast($P{toDate} as date)))

The aim is to generate a report in jasper depending on the values of the parameters. When the parameters 'fromDate' and 'toDate' are empty , the query should pull out the entire rows in the DB. How can I modify the query so that it accepts null values for 'fromDate' and 'toDate'.

XML File

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="customerSummary2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d23efc9c-641e-4e8a-bb9e-25673ee5c713">
<property name="ireport.zoom" value="1.610510000000001"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="accountNo" class="java.lang.String"/>
<parameter name="mobileNo" class="java.lang.String"/>
<parameter name="customerId" class="java.lang.String"/>
<parameter name="fromDate" class="java.util.Date">
    <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
</parameter>
<parameter name="toDate" class="java.util.Date">
    <defaultValueExpression><![CDATA[$P{fromDate}+7]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[select * from db_accessadmin.customerSummary where (accountNumber =         $P{accountNo} or $P{accountNo}='')
 and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
 and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
 and (cast(requestDate as date) between (cast($P{fromDate} as date)) and (cast($P{toDate} as date)))]]>
</queryString>

回答1:


This issue only occurs when i design the report in jasper iReport. But once i deploy it in the server, the issue is resolved. The server accepts null values.




回答2:


Change your query to:

 select * from db_accessadmin.customerSummary 
 where 
 (accountNumber = $P{accountNo} or $P{accountNo}='')
 and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
 and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
 $P!{date_sql}

Here's how I would do it in iReport, with Groovy, so you will have to modify this for Java. Create another parameter called date_sql, of type text, with the default value expression:

 ($P{fromDate}.isEmpty() || $P{fromDate} == null) 
 && ($P{toDate}.isEmpty() || $P{toDate} == null) 
 ? "and 1=1" : "and (cast(requestDate as date) between (cast("+$P{fromDate}+" as date)) and (cast("+$P{toDate}+" as date)))"

This is untested, so you might have to play around with the first part of the default value expression. But essentially, when the parameters are empty or null, return 1=1 (so, do not constrain by date) but if the parameters have values, use those values. What if your user enters one date but not the other? Maybe change the &&/AND to ||/OR and return 1=1 in that case.



来源:https://stackoverflow.com/questions/16460446/passing-empty-field-as-parameter-in-jasper-ireport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!