简体   繁体   中英

JasperReports API: How to pass multiple parameters to report

I am using JasperReports API to print from my Java application. I am printing a report like this:

public void prints(String billNo, String fileName, String outFileName, String perameterName) {
    HashMap hm = new HashMap();
    hm.put(perameterName, billNo);
    //Here parameterName is the parameter in JasperReport.
    // billNo is the value from my java application.

    try {
        conn = new connection().db();
        PrinterJob job = PrinterJob.getPrinterJob();
        /* Create an array of PrintServices */
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService default_service = PrintServiceLookup.lookupDefaultPrintService();
        int selectedService = 0;
        /* Scan found services to see if anyone suits our needs */
        for (int i = 0; i < services.length; i++) {
            if (services[i].getName().toUpperCase().equals(default_service.getName().toUpperCase())) {
                selectedService = i;
            }
        }
        job.setPrintService(services[selectedService]);
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(MediaSizeName.ISO_A4);
        printRequestAttributeSet.add(new Copies(1));
        JRPrintServiceExporter exporter;
        exporter = new JRPrintServiceExporter();

        JasperPrint print = JasperFillManager.fillReport(fileName, hm, conn);
        JRExporter exporterr = new JRPdfExporter();
        exporterr.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
        exporterr.setParameter(JRExporterParameter.JASPER_PRINT, print);

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();
        exporterr.exportReport();
    } catch (JRException e) {
        JOptionPane.showMessageDialog(null, e);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Printer not connected. Check power cable.");
    }
} 

I use to invoke this method in other classes.
But how can I pass multiple Parameters and multiple values to report? for example query:

"select * from table1 where date>"+thisDate+" and date<"+thatDate+" "

How can I send thisDate and thatDate from my Java application?

Since you are using a HashMap you can store there many values. You can do it like:

HashMap hm = new HashMap();  
hm.put(perameterName, billNo);
hm.put("thisDate", thisDate);
hm.put("theDate", theDate);
hm.put("anotherParam", paramValue);

If you don't want your method to pass so many keys and values as arguments, you can simply pass the "hm" object to your method:

public void prints(  String fileName, String outFileName, HashMap hm){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM