Domanda

I'm using Jasper Reports with Java to create some reports. In the interface, the user selects 1 or more items, and a report is generated for each item. Now, I have the query:

SELECT * FROM StockInventory

This is in the jrxml file. But, running that through my app, it'll create a report for every single item in the table. What I'd like is:

SELECT * FROM StockInventory WHERE pk IN (?...)

Where "?..." are the keys of the items the user selects. So, not only is the parameter dynamic, but the number of parameters is also dynamic.

My problem is, I don't know how to setup the parameters in the jrxml and I don't know how to set the parameters from within the jasperreports library in Java. Currently, to set values I'm doing this:

Map<String, Object> params = new HashMap<String, Object>();
JasperReport report = JasperCompileManager.compileReport("path\\to\\jrxml");
JasperPrint print = JasperFillManager.fillReport(report, params, new ItemData(keys));

Where ItemData() is :

private class ItemData implements JRDataSource
{
    private final List<InventoryItem> items;
    private int counter;

    public ItemData(List<PrimaryKey> keys)
    {
        items = new ArrayList<InventoryItem>();

        InventoryItemDao dao = new InventoryItemDao();
        for(PrimaryKey key : keys)
        {
            InventoryItem item = dao.find(key.getPk(), key.getCpk());
            if (item != null)
            {
                items.add(item);
            }
        }

        counter = -1;
    }

    @Override
    public boolean next() throws JRException 
    {
        if ( counter < items.size() - 1)
        {
            counter++;
            return true;
        }
        return false;
    }

    @Override
    public Object getFieldValue(JRField jrf) throws JRException 
    {
        // Return the relevant field
    }
}

The problem with this is that it's looping through the primary keys the user selected and running a query for each of those. If I could just do this with a single dynamic query, it would be much simpler.

Thanks for any help!

È stato utile?

Soluzione

To set parameters from Java, you have to put them in your params object.

Map<String, Object> params = new HashMap<String, Object>();
params.put("paramName",value);
JasperReport report = JasperCompileManager.compileReport("path\\to\\jrxml");
JasperPrint print = JasperFillManager.fillReport(report, params, new ItemData(keys));

and then use it in the report like $P{paramName}. Make sure, when you declare the parameter in the report to have the same name ('paramName') and that its class is the class of your value object.

If you want to use IN clause, you will have to use built-in SQL Clause Functions provided by Jasper.

The format is $X{IN, <column_name>, <parameter_name>} (make sure to use, in this case, just the name of the parameter, without $P).

So, in your case, the query would be:

SELECT * FROM StockInventory WHERE $X{IN,pk,paramName}

'paramName' parameter can be either a java.util.Collection instance or a java.util.List instance.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top