Create variable in iReport for the database

1

I need to create a report of the selected month with the information contained in the database, so I want to create a variable in iReport that receives the month (by netbens) which will be compared to the existing sqll in Ireport that is connected directly with the database .

Metrodo iReport:

public void imprimir() {
    try {

        JasperPrint print = JasperFillManager.fillReport("C:\Users\costa\OneDrive\Documentos\NetBeansProjects\Relatorio\Estoque.jasper", null, conexao);

        JasperViewer.viewReport(print, false);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

SQL present in iReport:

select * from Hospede h where DATE_FORMAT(h.horaini,'%m') = variavel
    
asked by anonymous 16.08.2017 / 16:41

1 answer

2

In this case you will have to create a parameter. In iReport do the following:

Itwillcreateanewparameternamed"parameter1", you will need to go into properties and edit the name and type of it.

To use it in your query the notation will be the following $ P {parametername}

select * from Hospede h where DATE_FORMAT(h.horaini,'%m') = $P{nomeparametro}

In the java code:

HashMap params = new HashMap<>();
params.put("nomeparametro", parametro);
JasperPrint print = JasperFillManager.fillReport("C:\Users\costa\OneDrive\Documentos\NetBeansProjects\Relatorio\Estoque.jasper", params, conexao);
    
16.08.2017 / 17:50