5

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 ?

3 Answers 3

18

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}
Sign up to request clarification or add additional context in comments.

2 Comments

thankyou. the problem is now solved. just wanted to ask one more thing. Suppose i want to print the parameter i.e $P{id} in jasper report. (right now we are using it in a query.). I simply want to print the parameter in the report. Is it possible.. is yes .please help .
You should mark the question as Answered if your problem is solved. In i-Report Choose Section where you want to print.Define a field $F{id}.BTW this is another question. :)
1

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>

1 Comment

Maybe calling a service layer with the parameter needed to update the jasperreport report is better programming pattern, that's why I have downvoted your post.
0

you can't select *, you have to indicate the column name that you want to get data from. use commDB.query to execute query, then pass result to commDBResult, run the loop, put each row of record into an array list, then use jasper to generate report

1 Comment

Thankyou. I have successfully generated the report. The problem i am facing is my report is static. I want a dynamic report. Means i want to enter the value of id (for example) from jsp/java which changes the query and corresponding report is generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.