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 ?
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}
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
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>