Wednesday, 2 January 2013

Calling a Stored Procedure from Jasper Reports

Calling Oracle Stored Procedure in JasperReports

We can execute a Stored Procedure in Jasper as PLSQL is supported in Jasper.
Prerequisites 
  •  The procedure should have an out parameter of Cursor type.
  • Any inputs to procedure should be sent as in parameters.
  • We need to define field names in iReport Designer manually that exactly match the field names that are returned by cursor
Step1:  Create a blank page in iReport Designer.
 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEix4zFkHL-OFrhYM80NT1gKzcxPJYA40lXvGjasOjNLiWDu-P6OGEXC9IWk1W9ksv0MbpP5jCEy3NiFvfDk7xsrW9zkStbwWQX8ao-lrEaI2a47FyGr0spH4b9A7SJygDpbdXiOu_8L3jyt/s640/1.jpg
Step2:  We need to add PLSQL Executor in order to execute PLSQL blocks.  In Latest versions of ireport Designer, it will be already added. But if you are using earlier version, you need to add the following properties.
Go to Tools>>Options>>Query Executers 
Language =plsql
Factory Class =com.jaspersoft.jrx.query.PlSqlQueryExecuterFactory
Fields Provider Class =com.jaspersoft.ireport.designer.data.fieldsproviders.SQLFieldsProvider
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFwL7BMe6KImH73_Pmfpt2VwO9zq__DJCoxyIRX6suGtoKxYggVlWrNAaOnZFCu0-vdqUtcAhiW4S07ALpIgnPKRM0kHCNXWdQlcMn58X3_t6lTctHJXkFV3XOhaRbdhZPnlojSnTJBlz0/s640/2.jpg
Make sure that you add jasperreports-extensions-3.5.3.jar  and ojdbc14.jar files in classpath
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBqz4xqK1_y8Nk-U0yZMs89YGddGwmGh2jO5KJBxYY4mgs3J4q0EO8K2rLOGhOjAqUrXtgCGfQM1zL003RuGS-hYcm5Ca9E9dYm4UpoIhSxjs958i5DuUlJF1ngV3BO6GgF8ZuBdrtQ0dy/s640/3.jpg
Step 3:  Open  report Query  and select Query Language = “plsql”. If you observe the available parameters on the right hand side, you will find ORACLE_REF_CURSOR parameter which is going to be the out parameter for your procedure. Now write your PLSQL code inside curly braces {}.
{call PKG_REPORTS_API.get_shop_details_report ($P{memberNumber},$P{orderNumber}, $P{ORACLE_REF_CURSOR})}

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVNgw9p_vmrZfo0xNgax5AlIfh_auc9jjOEbnlDR41WwZ64385ZkcUmo5yyp14-11cMpzqQN9s0cMnO1g59lzPlK0J8mC77MuUfmVgVYPQ6XCtOPT8pRSfbtmKS6l5aPAiMHDPIKFylWli/s640/4.jpg
If you are using old versions you can create your own parameter name “cursor” with datatype as “java.sql.ResultSet” and also make sure “use for prompt” is not selected.
Step 4:  Now use the fields in your report and execute the report to see the results.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJwE5dulznO4Qptji6p4DUAa2f6CliO4Fkk4sg2e5Z5mSXj-_oozvKBRPhMQ-x6hiR5LpIqR3UYn55wFWc0D1cKauJYVv9rt_183GoBMphSeOBwiR7bvAR-WY6TfZEEvF2n1xkVFxGyi74/s640/5.jpg
Results Screen
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfn73TOyh56bkMeU7siUQ86NKlzz-cM7AO1cyGm64SxeKyW9cHD0wDf_6d_IyWMykvhrgAHMp6zPRZI4Ddmob9dIhZLC0xBLX_sj9ekNlwoChP5K3LrApVG8KLqLgVIIZMcRuwNN_qyn3B/s640/6.jpg

4 comments:

  1. If you are using odbc driver will show the following error: Error: SQL problems: Gerenal error. ressolver to the problem use the jdbc driver

    ReplyDelete
  2. Is there a similar solution for TIBCO Jaspersoft® Studio?

    ReplyDelete