birt系列(4)--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();
			}
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值