Error generating report in Jaspersoft Studio due t

2019-08-31 00:48发布

问题:

I am trying to generate several jasper reports in Eclipse using the Jaspersoft Studio plugin.

I have 4 parameters that will be passed in, and i am manually adding the input parameters myself.

When i run the preview it gives me Error generating report due to

'Error preparing statement for executing the report query: 
SELECT rcia.inquirer.`First_Name`,
 rcia.inquirer.`Middle_Name`,
 rcia.inquirer.`Last_Name`,
 rcia.inquirer.`Father_Full_Name`,
 rcia.inquirer.`Mother_Full_Name`,
 rcia.inquirer.`Sponsor_First_Name`,
 rcia.inquirer.`Sponsor_Last_Name`,
FROM rcia.inquirer
WHERE 
 rcia.inquirer.`First_Name` = '$P{FirstName}' 
 AND rcia.inquirer.`Last_Name` = '$P{LastName}''

I am not sure why i am getting this error. Am using a correct SELECT query? I am passing in the parameter FirstName and LastName and using them to select all of the data in the database.

回答1:

While executing query in jasper report you can use:

  1. Prepared statement (avoid sql injection) this is achieved by using

    $P{FirstName}
    

    The query in this case need's to be without qualifier '

    rcia.inquirer.`First_Name` = $P{FirstName} 
    
  2. String substitution (as @mkl comment)

    $P!{FirstName}
    

    The query in this case need's the qualifier (since its a simple string substitution)

    rcia.inquirer.`First_Name` = '$P!{FirstName}'
    

The preferred way is Prepared statement mainly because this will help you to avoid sql injection, but also it will help you to avoid errors if the firstName for example contains ' es. Al'Capone, or other chars that could break your query es. \ ecc.