简体   繁体   中英

Jasper Reports: How to pass multiple SQL queries from a Java Program

I have constructed a Jasper Report using iReport tool, where in I have registered two data sets, one to polulate the data in a table, and other to display a chart.

The configuration was successful using the tool and when i see the report it gives me proper data. How ever when i try to invoke the queries from a Java program I am lost. How do i go about handling this? I can only pass a single query with the sample source code I have in as my program.

Sample Source code I am using:

            Connection conn = getConnection("172.16.88.171", "1522", "orcl", "audi", "audi");
        System.out.println("Got jdbc connection...");
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("SELECT DB_USER, OS_USER, USERHOST, STATEMENT_TYPE, SQL_TEXT FROM DBA_FGA_AUDIT_TRAIL");


        InputStream input = new FileInputStream(new File("E:\\jasper_reports\\sampleADPTemplate_chart.jrxml"));
        JasperDesign design = JRXmlLoader.load(input);
        JasperReport report = JasperCompileManager.compileReport(design);

        JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), conn);

        OutputStream output = new FileOutputStream(new File("E:\\jasper_reports\\JasperReporttoPDF.pdf"));
        JasperExportManager.exportReportToPdfStream(print, output);

This only passes a single query; How do i go about passing multiple queries.

Thanks.

You only need to store your queries entirely inside your iReport jrxml file. In order to make your report flexible, use parameters. This way you allow the user to define the needed values at runtime.

Here you can see an example that gets two values from two combo-boxes, adds them to a HashMap and passes the map to iReport. These paramaters, for this example " storeName " and " actionCode ", are used to specify values for the query which is stored inside the iReport.

You can have multiple queries inside Sub-Reports.

try {

    String shopName = jComboBox1.getSelectedItem().toString();
    String actionCode = jComboBox2.getSelectedItem().toString();

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("storeName", shopName);
    map.put("actionCode", actionCode);

    URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
    File reportFile = new File(reportFileURL.toURI());
    JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);

    JasperViewer jv = new JasperViewer(jasperPrint);
    JDialog viewer = new JDialog(this, "Batch Report", true);
    viewer.setBounds(jv.getBounds());
    viewer.getContentPane().add(jv.getContentPane());
    viewer.setResizable(true);
    viewer.setIconImage(jv.getIconImage());
    viewer.setVisible(true);

} catch (JRException exc) {
   System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
   System.out.println(exs.getMessage());
} 

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