How to set a datasource for a BIRT report programmatically?

后端 未结 5 1079
借酒劲吻你
借酒劲吻你 2021-02-04 08:46

I have a BIRT report which connects to our test database. In the productive environment I would like to supply a datasource which is provided by the container through jndi.

5条回答
  •  感动是毒
    2021-02-04 09:37

    I like Adams approach. Here's how we do it:

        /*
     * Change the data sources in the .rptdesign
     */
    void changeDataSource(ElementFactory designFactory,
            ReportDesignHandle designHandle, String userConnect)
            throws SemanticException {
    
        SlotHandle datasources = designHandle.getDataSources();
        SlotIterator iter = (SlotIterator) datasources.iterator();
        while (iter.hasNext()) {
            DesignElementHandle dsHandle = (DesignElementHandle) iter.next();
            if (dsHandle instanceof OdaDataSourceHandle && dsHandle.getName().equals("lisa")) {
                log.debug("changeDataSource: Changing datasource "
                        + dsHandle.getName() + " new url=" + getLisaDbUrl());
                dsHandle.setProperty("odaDriverClass",
                        "oracle.jdbc.driver.OracleDriver");
                dsHandle.setProperty("odaURL", getLisaDbUrl());
                dsHandle.setProperty("odaUser", getLisaUser());
                dsHandle.setProperty("odaPassword", getLisaPassword());
            } else {
                log.debug("changeDataSource: Ignoring DS " + dsHandle.getName());
            }
        }
    }
    

    Here, "lisa" ist he name of the datasource we would like to change at runtime. The get... function return the values needed at "production" run time.

提交回复
热议问题