How to generate an ireport according user input in netbean gui

青春壹個敷衍的年華 提交于 2019-12-25 01:53:55

问题


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 ?


回答1:


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:

And here you go some tutorials:1 and 2

Hope these helps!



来源:https://stackoverflow.com/questions/22345275/how-to-generate-an-ireport-according-user-input-in-netbean-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!