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

I have successfully created a report and have successfully exported to HTML and PDF . However, this is a static report. I have a request like:

 select * from personal where id= 'val' 

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

+4
source share
3 answers

Create a map containing the parameters and place the parameters as a key value pair.

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

When creating a Jasper report from JSP:

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

where the keys in the parametersMap shoud will exactly match the parameters defined in the report template.

So, declare the parameter in the report template (jrxml):

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

Skip parameter in query in Jasper report

 select * from personal where id= $P{id} 
+14
source

you cannot select *, you must specify the name of the column from which you want to get data. using commDB.query to execute the query, then pass the result to commDBResult, start the loop, put each row of the record in a list of arrays, and then use jasper to generate the report

0
source

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> 
0
source

Source: https://habr.com/ru/post/1403408/


All Articles