Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter value in JasperReport from JSP or Java code?

I have successfully created a report and successfully exported to HTML and PDF. However it is a static report. I have a query as:

select * from personal where id= 'val' 

I want to send this parameter "val" from Java/JSP at runtime. How to do this ?

like image 972
vicky Avatar asked Mar 26 '12 06:03

vicky


2 Answers

Create a Map containing parameters and put parameters as key value pair.

Map parametersMap = new HashMap();  
parametersMap.put("id",7);

When generating Jasper Report from JSP:

JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parametersMap, jdbcConnection);

where the keys in the parametersMap shoud be excatly the same as the parameters defined in your report template.

So, Declare the parameter in your report template (jrxml):

<parameter name="id" class="java.lang.Integer"/>

Pass parameter in query in Jasper Report

select * from personal where id= $P{id}
like image 156
Hardik Mishra Avatar answered Oct 22 '22 19:10

Hardik Mishra


This will be your code for jsp.

<%@page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import="net.sf.jasperreports.view.JasperViewer"%>
<%@page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@page import="net.sf.jasperreports.engine.JasperReport"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JRResultSetDataSource"%>
<%@page import="net.sf.jasperreports.engine.JasperCompileManager"%>
<%@page import="net.sf.jasperreports.*"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
<%
           try
           {
       JasperReport jasperReport=JasperCompileManager.compileReport("PASS LOCATION TO YOUR .JRXML FILE");
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn =   DriverManager.getConnection("jdbc:mysql://localhost:3306/t_fleet" , "root", "root");
        Integer inv_no=0;
        Statement stmt = null;
        ResultSet rset = null;
        Statement st2=conn.createStatement();
        String queryString = "PASS YOUR QUERY HERE";
        stmt = conn.createStatement();
        rset = stmt.executeQuery(queryString);
        JRResultSetDataSource jasperReports = new JRResultSetDataSource(rset);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null, jasperReports);
        //JasperViewer.viewReport(jasperPrint); 

        String filename=null;
        filename="SET NAME TO YOUR FILE NAME AND APPEND.pdf TO IT";

        //Report saved in specified path
        JasperExportManager.exportReportToPdfFile(jasperPrint,filename);

        //Report open in Runtime
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +filename);
       }
       catch(Exception e)
       {
           out.println(e);
       }
    %>

    </body>
    <script>

     </script>
</html>
like image 23
Aditya Ekbote Avatar answered Oct 22 '22 19:10

Aditya Ekbote