webminer07 webminer07 - 3 months ago 50
Java Question

How to pass parameters to JasperReport with java to use later in SQL query

I already have 6 Jasper Report templates created, with all of the static text fields to be filled out using a SQL query. The SQL query uses 2 parameters that I am passing in: FirstName and LastName. I am also passing in 2 other parameters that will just get added to Report.

This is the code i have so far to pass the HashMap with the parameters to the Report. But I am lost as there isnt really any good documentation or examples that I can find.

package print;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;

public class PrintCertificate
public PrintCertificate(String output, String certType, String firstName, String lastName, String confirmDate, String pastorName)
String fileName = "/RCI_Eng.jasper";
output = "C:/Users/User/Desktop/Test/";

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("PastorName", pastorName);
map.put("DateOfConfirmation", confirmDate);
JasperPrint print = JasperFillManager.fillReport(fileName, map);
JRDocxExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test.docx");

catch (Exception e)

I know this is probably far from correct, but if someone can point me in the right direction of good documentation or examples, or point out what I've done wrong, it would be of great help!


Here is a brief description to use Jasper Report with your Java Application

  • First you have to set database connection to the Report.

enter image description here

  • Then you can design your SQL query for the report.

    enter image description here You can add parameters to the SQL query in case you need to filter data through your query.Just add parameters using New Parameter button and you can drag and drop parameters displaying inside the text area to your query.

In your report inspector you can see all the column names of our query under Fields category and all the parameters defined under Parameters category.

enter image description here

  • You can design a basic report like below

    By dragging and dropping field names and parameters to your Report Designer you can design your report as you desire. Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) and Summary Band(Like Grand_Total,Date,Issued By and chart elements can be added to this section) are enough for basic report.

    enter image description here
  • Then you can declare ReportGenarator class to generate your report

    public class ReportGenarator {
    public static String OUT_PUT = "your_output_file_path/myreport.docx";
    public static String REPORT = "your_report_path/myreport.jrxml";
    public void genarateReport(String reportPath,
            Map<String, Object> map, Connection con) {
        try {
            JasperReport jr = JasperCompileManager.compileReport(
            JasperPrint jp = JasperFillManager.fillReport(jr, map, con);
            JRDocxExporter export = new JRDocxExporter();
        export.setExporterInput(new SimpleExporterInput(jp));
        export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT)));
        SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
        } catch (JRException ex) {
    } }
  • You can generate your report by Pressing a button in your application.So that you have to include below code inside your button action event

    Map<String, Object> map = new HashMap<>();
                map.put("headding", "REPORT FROM DATABASE CONNECTION");//parameter name should be like it was named inside your report.
                new ReportGenarator().genarateReport(
                        ReportGenarator.REPORT, map, your_DB_connction_reference_here);