Search code examples
javajasper-reports

Calling a Jasper Studio report from Java


Jaspersoft advertises the ability to "embed" reports into your Java application. I think I'm missing some fundamental step...

I can create reports through a JDBC connection to MySQL. It runs fine. It generates the .jrxml file, but then what? How do I call this report from Java? (I use Netbeans). Are there JasperReports libraries I need to import? Or do I have to also install the report server? I was hoping to write a report, embed it into the code, and be able to run that off any computer (not just one that has the report server installed). Is that possible? How do I call the report and pass in the parameters? Something like

JasperReport jr = new JasperReport(param1, param2)

Something along those lines? Possible?


Solution

  • JasperReport jasperReport = JasperCompileManager.compileReport("path/filename.jrxml");
    
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("parameter_name", value); //only if you want to pass any parameters
    
    JRDataSource dataSource = new JREmptyDataSource(); //your db connection
    
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
    

    Now you have a JasperPrint which you can export, show and print.

    You can also refer to JasperReports Ultimate Guide for more details.