欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

birt报表自定义数据源

程序员文章站 2022-04-30 20:37:54
...

birt报表自定义数据源

 

 handler相关类中,关键是动态设置IDataSourceInstance dataSource,相关处理代码如下:

这段代码实现功能是:优先用报表管理的数据源,如果报表本身没有设置数据源,则采用公共数据源(配置文件中配置)

public class BirtDataSource extends DataSourceEventAdapter {
	private ReportDao repDao = new ReportDao();
	//ReportDesignHandle designHandle = null; // 报表设计引擎
	//ElementFactory designFactory = null; // 元素工厂
	//StructureFactory structFactory = null;

	@Override
	public void beforeOpen(IDataSourceInstance dataSource,
			IReportContext reportContext) throws ScriptException {
		super.beforeOpen(dataSource, reportContext);
		String name = reportContext.getReportRunnable().getReportName();
		String reportName = name.substring(name.lastIndexOf("/")+1);
		
		System.out.println("【BirtDataSource】当前连接报表的名称为:"+reportName);
		
		
		
		
		DataSource dt = repDao.getDataSourceByReportName(reportName);
		dataSource.setExtensionProperty("odaURL", dt.getDataSourceUrl());
		dataSource.setExtensionProperty("odaUser", dt.getDataSourceUserName());
		dataSource.setExtensionProperty("odaPassword", dt.getDataSoucePassword());
		dataSource.setExtensionProperty("odaDriverClass",dt.getDataSourceDriver());
		
		System.out.println("【BirtDataSource】当前报表连接的数据源为:"+dt.getDataSourceName());
		
		if(dt.getDataSourceName()==null&&dt.getDataSoucePassword()==null&&dt.getDataSourceDriver()==null){
			System.out.println("【BirtDataSource】多数据源为空,直接读取dbconfig2.properties");
			
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dbconfig2.properties");
			Properties p = new Properties();
			try {
				p.load(inputStream);
				String username = p.getProperty("username");
				dataSource.setExtensionProperty("odaURL", p.getProperty("url"));
				dataSource.setExtensionProperty("odaUser", username);
				dataSource.setExtensionProperty("odaPassword", p.getProperty("password"));
				dataSource.setExtensionProperty("odaDriverClass", p.getProperty("driver"));
				System.out.println("读取dbconfig2.properties。。。。。。。");
				//判读连接是否可用
				Class.forName(p.getProperty("driver"));
				Connection conn = DriverManager.getConnection(p.getProperty("url"),username,p.getProperty("password"));
				if(conn == null){
					System.out.println("【BirtDataSource】当前报表连接不上数据库,请检查数据库连接");
				}else{
					conn.close();
				}
				
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (SQLException e) {
				System.out.println("【BirtDataSource】:"+e.getMessage());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

 

相关标签: 报表