Variable Expressions

Hi all,

I am currently trying to create a report based that displays the number of pages that I am printing per month. My SQL query is pulling all the data in my database since recording of the printer page count started. Creating a variable which calculates the total is easy enough, but is there a way that a variable can select specific data from the sql query and use that in a sum calculation, i.e. just this month's data? Could I use a sql quert within the variable expression is some way?


jcowpland's picture
Joined: Nov 11 2011 - 7:40am
Last seen: 11 years 10 months ago

2 Answers:

you could try something like the following in the Variable Expression property:

new java.text.SimpleDateFormat("MM").Format($F{fieldDate}) == new java.text.SimpleDateFormat("MM").Format(new java.util.Date()) ? $F{field you want to sum} : 0

might be a more simpler way to do this, but it's the "IF" stmt basically...
lknueve's picture
Joined: Jan 10 2011 - 3:00pm
Last seen: 12 years 8 months ago

Thanks for the help.

I changed the variable expression field to:

new java.text.SimpleDateFormat("MM").Format($F{sampletime})==new java.text.SimpleDateFormat("MM").Format(new java.util.Date()) ? $F{samplevalue} : 0

but it then produces the following error. Error filling print... Error evaluating expression :      Source text : new java.text.SimpleDateFormat("MM").Format($F{sampletime})==new java.text.SimpleDateFormat("MM").Format(new java.util.Date()) ? $F{samplevalue} : 0
Setting up the file resolver...  net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :      Source text : new java.text.SimpleDateFormat("MM").Format($F{sampletime})==new java.text.SimpleDateFormat("MM").Format(new java.util.Date()) ? $F{samplevalue} : 0      at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(      at net.sf.jasperreports.engine.fill.JRCalculator.evaluateEstimated(      at net.sf.jasperreports.engine.fill.JRCalculator.estimateVariables(      at      at      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(      at net.sf.jasperreports.engine.fill.JRFiller.fillReport(      at net.sf.jasperreports.engine.JasperFillManager.fillReport(      at net.sf.jasperreports.engine.JasperFillManager.fillReport(      at      at org.openide.util.RequestProcessor$      at org.openide.util.RequestProcessor$  Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find function Format in object java.text.SimpleDateFormat@9a0.      at org.mozilla.javascript.ScriptRuntime.constructError(      at org.mozilla.javascript.ScriptRuntime.constructError(      at org.mozilla.javascript.ScriptRuntime.typeError(      at org.mozilla.javascript.ScriptRuntime.typeError2(      at org.mozilla.javascript.ScriptRuntime.notFunctionError(      at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(      at org.mozilla.javascript.gen.c120._c0(expression:0)      at      at org.mozilla.javascript.ContextFactory.doTopCall(      at org.mozilla.javascript.ScriptRuntime.doTopCall(      at      at org.mozilla.javascript.gen.c120.exec(expression)      at net.sf.jasperreports.compilers.JavaScriptEvaluator.evaluateExpression(      at net.sf.jasperreports.compilers.JavaScriptEvaluator.evaluateEstimated(      at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(      ... 13 more 
Print not filled. Try to use an EmptyDataSource...


I'm fairly new to java and sql so it may be that I've just gotten something really simple wrong. Apolgies if that's the case.



jcowpland's picture
Joined: Nov 11 2011 - 7:40am
Last seen: 11 years 10 months ago