[英]Pass connection from Java Application to Birt report
我是Birt的新手。
我正在尝试通过Java应用程序将连接传递到报表,但出现错误:
以下各项有错误:
ReportDesign(id = 1):+评估脚本“ importPackage(Packages.it.lfiammetta.birt); var conn = new ReportRenderer(); reportContext.getAppContext()。put(” OdaJDBCDriverPassInConnection“,conn);”时出错:无法在函数__bm_beforeOpen()中执行脚本。 资源:
“ + importPackage(Packages.it.lfiammetta.birt); var conn = new ReportRenderer(); reportContext.getAppContext()。put(” OdaJDBCDriverPassInConnection“,conn); +”
发生了BIRT异常。 有关更多信息,请参见下一个异常。 评估Javascript表达式时出错。 脚本引擎错误:ReferenceError:未定义“ ReportRenderer”。 (/ report / data-sources / oda-data-source [@ id =“ 43”] / method [@ name =“ beforeOpen”]#2)脚本源:/ report / data-sources / oda-data-source [ @ id =“ 43”] / method [@ name =“ beforeOpen”],行:0,文本:__bm_beforeOpen()。 (元素ID:1)
这是我的Java代码,用于创建和启动报告:
package it.lfiammetta.birt;
public class ReportRenderer {
public void executeReport() {
code...
Map<String, Object> appContext = task.getAppContext();
appContext.put("OdaJDBCDriverPassInConnection", myConnection);
appContext.put("OdaJDBCDriverPassInConnectionCloseAfterUse", false);
task.setAppContext(appContext);
task.run();
code...
}
}
这是我在脚本“ beforeOpen”数据源中编写的代码:
importPackage(Packages.it.lfiammetta.birt);
var conn = new ReportRenderer();
reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn);
我设置了类路径。
我使用的Birt版本是4.2.1。
在此先感谢您的帮助,对于我的英语致歉。
我正在从Java代码中执行此操作( IJDBCParameters
实际上是JDBC连接的参数,我正在按名称查找连接OdaDataSourceHandle.getName()
):
@SuppressWarnings("rawtypes")
private static void substituteJDBCConnections(IReportRunnable pReportRunnable) {
final Map<String, IJDBCParameters> jdbcConnections = reportParameters.getJdbcConnections();
if (jdbcConnections != null ){
for (Iterator iter = pReportRunnable.getDesignHandle().getModuleHandle().getDataSources().iterator(); iter.hasNext();){
// http://wiki.eclipse.org/Java_-_Execute_Modified_Report_(BIRT)
Object element = iter.next();
if (element instanceof OdaDataSourceHandle){
OdaDataSourceHandle dsHandle = (OdaDataSourceHandle) element;
String key = dsHandle.getName();
if (key == null){
continue;
}
IJDBCParameters jdbcParams = jdbcConnections.get(key);
if (jdbcParams == null){
continue;
}
try {
dsHandle.setProperty( "odaDriverClass", jdbcParams.getDriverName());
dsHandle.setProperty( "odaURL", jdbcParams.getConnectionString());
dsHandle.setProperty( "odaUser", jdbcParams.getUserName());
dsHandle.setProperty( "odaPassword", jdbcParams.getPassword());
} catch (SemanticException e) {
throw new UncheckedException(e);
}
}
}
}
可能您已经解决了问题,但是将来可能会有人在寻找这个问题。 首先,4.2.x版本的连接存在问题。 我没有在4.4.2中观察到相同的错误。
另一件事,我不明白为什么您要尝试通过ReportRenderer
作为行中的连接:
var conn = new ReportRenderer();
reportContext.getAppContext().put("OdaJDBCDriverPassInConnection", conn);
此处传递的对象应该是java.sql.Connection
对象。
因此,
Map<String, Object> appContext = task.getAppContext();
appContext.put("OdaJDBCDriverPassInConnection", myConnection);
appContext.put("OdaJDBCDriverPassInConnectionCloseAfterUse", false);
task.setAppContext(appContext);
只要myConnection
是java.sql.Connection
的实现,就看起来正确
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.