Sep 12

如何用程序指定Birt报表的JDBC Connection 不指定

chirs , 08:46 , 瓜哇天地 , 评论(0) , 引用(0) , 阅读(2834) , Via 本站原创 | |
    Birt报表制作时会记录了jdbc连接信息(如果用jdbc连接),运行时直接用这连接信息取数据生成报表!但很多时候我们运行报表的环境和制作报表环境不一样,连接信息不一样或不确定,这样需要由程序来控制jdbc的Connection,可惜Birt的Report Engine没有提供方便的接口来实现我们的需求!
     Jason在“Birt world” blog中标题为“Using a supplied connection with BIRT”提供了很好的解决方案,可以通过修改oda jdbc plugins和IRunTask的setAppContext来实现程序控制数据连接方式。
     我根据实习需要把代码进行了相应修改,两个类分别继承于org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver和Connection:

public class JdbcExistDriver extends OdaJdbcDriver{
    private final static String APPLICATION_BIRT_CONNECTION = "webreport.birt.connection";
    private Connection conn = null;
    public JdbcExistDriver() {
    }
    public IConnection getConnection(String string) throws OdaException {
        if(this.conn != null){
            ExistConnection jdbcConn = new ExistConnection();
            jdbcConn.conn = conn;
            return jdbcConn;
        }else{
            return new org.eclipse.birt.report.data.oda.jdbc.Connection();
        }
    }
    public void setAppContext(Object context) throws OdaException {
        Map ctx = (Map)context;
        this.conn = (Connection)ctx.get(APPLICATION_BIRT_CONNECTION);
    }
    public void setLogConfiguration( LogConfiguration logConfig )
                    throws OdaException{
        super.setLogConfiguration(logConfig);
    }
}

public class ExistConnection extends Connection {
    protected java.sql.Connection conn = null;
    public ExistConnection() {
        super();
    }
    public void close() throws OdaException {
        if(this.jdbcConn != null){
            this.jdbcConn = null;
        }
    }
    public void open(Properties properties) throws OdaException {
        this.jdbcConn = conn;
    }
}

copy 两个类到WEB-INF\platform\plugins\org.eclipse.birt.data.oda.jdbc_XXXXX\oda-jdbc.jar里;
修改plugin.xml文件,把
<dataSource
            odaVersion="3.0"
            driverClass="org.eclipse.birt.report.data.oda.jdbc.OdaJdbcDriver"
            defaultDisplayName="%datasource.name"
            setThreadContextClassLoader="false"
            id="org.eclipse.birt.report.data.oda.jdbc">

改为
<dataSource
            odaVersion="1.0"
            driverClass="xx.JdbcExistDriver"
            defaultDisplayName="exit driver"
            setThreadContextClassLoader="false"
            id="org.eclipse.birt.report.data.oda.jdbc">

程序调用修改为:
context.put("webreport.birt.connection",conn);
Tags: , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]