如何用程序指定Birt报表的JDBC Connection
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() {
}
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();
}
}
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);
}
Map ctx = (Map)context;
this.conn = (Connection)ctx.get(APPLICATION_BIRT_CONNECTION);
}
public void setLogConfiguration( LogConfiguration logConfig )
throws OdaException{
super.setLogConfiguration(logConfig);
}
}
throws OdaException{
super.setLogConfiguration(logConfig);
}
}
public class ExistConnection extends Connection {
protected java.sql.Connection conn = null;
public ExistConnection() {
super();
}
protected java.sql.Connection conn = null;
public ExistConnection() {
super();
}
public void close() throws OdaException {
if(this.jdbcConn != null){
this.jdbcConn = null;
}
}
if(this.jdbcConn != null){
this.jdbcConn = null;
}
}
public void open(Properties properties) throws OdaException {
this.jdbcConn = conn;
}
}
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">
改为
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);
odaVersion="1.0"
driverClass="xx.JdbcExistDriver"
defaultDisplayName="exit driver"
setThreadContextClassLoader="false"
id="org.eclipse.birt.report.data.oda.jdbc">
程序调用修改为:
本文介绍了一种在BIRT报表中使用自定义JDBC连接的方法。通过修改特定的类和配置文件,可以实现在不同环境中灵活指定数据库连接,解决因环境变化导致的连接问题。
2366





