Question

CREATE TABLE Account(
    account_no int,
    balance real,
    type_code int,
    branch_no int,
    Bcode int,
    customer_no int,

    CONSTRAINT account_pk PRIMARY KEY (account_no),

);

I want to generate a Jasper Report when user gives the type_code(account type code) or branch_no as GUI inputs, then the ireport should display all the account details of above account table.

I have no idea how to do this. can anyone help me ?

Was it helpful?

Solution

First of all you didn't specify the kind of application you want to create, so i'm being a bit general on my response. Also you didn't mention if you've already managed to build your first report (i mean, without taking any user input). So, below i'm showing the needed part for generating a JasperReport:

public void generateReport(ActionEvent actionEvent) throws FileNotFoundException {

JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(PopulateBean.createBeanCollection());
Map parameters = new HashMap();
 
try {
    InputStream is = new FileInputStream(new File("Source path to template.jrxml"));
    OutputStream os=new FileOutputStream(new File("Resulting report.pdf"));
     
    JasperDesign jasperDesign = JRXmlLoader.load(is);
    JasperReport jasperReport =
        JasperCompileManager.compileReport(jasperDesign);

    JasperPrint jasperPrint =
        JasperFillManager.fillReport(jasperReport, parameters, ds);

    JasperExportManager.exportReportToPdfStream(jasperPrint, os);
} catch (JRException e) {
      e.printStackTrace();
}

}

This code should be integrated into your application. The part you are asking for is:

Map parameters = new HashMap();

You have just to put the input inserted by the user into this map. Example, if you have a JSF page, then you can take the value of it's UI component and store in this map

parameters.put("type_code", getTypeCodeUIComponent().getValue());

You'll see on the code above that this map is passed to the report:

JasperFillManager.fillReport(jasperReport, parameters, ds);

The only thing that remains is to edit your report query, in iReport. First you create a parameter with the exact same name as that inserted into the map (in this example "type_code". Note, it's case sensitive). Second, you should use a WHERE clause where you filter the type column based on this parameter, see below: passing parameters And here you go some tutorials:1 and 2

Hope these helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top