有用過這一套的人大概都知道, 設計完一個報表後會產生一個 .rptdesign 的檔案.
而一般在程式中呼叫此報表檔, 並繫結資料的程式碼大致如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | IReportRunnable design = this .birtReportEngine.openReportDesign( "test_report.rptdesign" ); IRunAndRenderTask task = this .birtReportEngine .createRunAndRenderTask(design); // set report parameters task.setParameterValue( "startTime" , "2010-09-01" ); task.setParameterValue( "endTime" , "2010-12-01" ); // set output format HTMLRenderOption options = new HTMLRenderOption(); options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); options.setOutputStream(response.getOutputStream()); task.setRenderOption(options); // run the report task.run(); task.close(); |
(將 .rptdesign 用記事本之類的文字編輯器打開可以看到如下的設定)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < data-sources > < oda-data-source extensionID = "org.eclipse.birt.report.data.oda.jdbc" name = "ReportDS" id = "21" > < list-property name = "privateDriverProperties" > < ex-property > < name >contentBidiFormatStr</ name > < value >ILYNN</ value > </ ex-property > < ex-property > < name >metadataBidiFormatStr</ name > < value >ILYNN</ value > </ ex-property > </ list-property > < property name = "odaDriverClass" >net.sourceforge.jtds.jdbc.Driver</ property > < property name = "odaUser" >reporter</ property > < encrypted-property name = "odaPassword" encryptionID = "base64" >xxxxxxx</ encrypted-property > </ oda-data-source > </ data-sources > |
一般來說, 開發環境的連線設定跟正式環境應該是不一樣的. 而且資料庫的存取帳號大概也只有 DBA 知道. 除非 DBA 已經跟開發者講好帳密等連線資訊, 不然這樣的報表檔案一套用下去大概在連線上就先掛了.
所以我大略整理以下兩種方式, 讓報表在 Runtime 時可以設定新的連線資訊.
- 如果連線資訊是放在參數檔或某個特定的 class 中, 可以用以下的設定方式將既有的連線資訊蓋掉:
以上的連線值都可以從別的來源取得並設定.123456789
IReportRunnable design =
this
.birtReportEngine.openReportDesign(
"test_report.rptdesign"
);
IDataSource ds = design.getDesignInstance().getDataSource(
"ReportDS"
);
ds.setPrivateDriverProperty(
"odaDriverClass"
,
"net.sourceforge.jtds.jdbc.Driver"
);
ds.setPrivateDriverProperty(
"odaURL"
,
ds.setPrivateDriverProperty(
"odaUser"
,
"reportAcc"
);
ds.setPrivateDriverProperty(
"odaPassword"
,
"productionpw"
);
// 以下省略...
- 如果系統本身就已經有使用 3rd 的 connection pool 套件, 讓報表取用 connection pool 中的 connection 物件來使用應該是較佳的做法.
以下是我用 proxool 的設定方式( proxool 的設定方式都略過了)只要簡單地將 Connection 物件設定至 task 的 "OdaJDBCDriverPassInConnection" 裡就可以讓報表從這個連線去撈取資料並進行繫結.123456789101112131415IReportRunnable design =
this
.birtReportEngine.openReportDesign(
"test_report.rptdesign"
);
IRunAndRenderTask task =
this
.birtReportEngine
.createRunAndRenderTask(design);
// set report parameters
task.setParameterValue(
"startTime"
,
"2010-09-01"
);
task.setParameterValue(
"endTime"
,
"2010-12-01"
);
// set output format
HTMLRenderOption options =
new
HTMLRenderOption();
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
options.setOutputStream(response.getOutputStream());
task.getAppContext().put(
"OdaJDBCDriverPassInConnection"
, DriverManager.getConnection(
"proxool.reportdb"
));
task.setRenderOption(options);
// run the report
task.run();
task.close();
有關 ODA 的說明, 可以從這邊開始:
http://wiki.eclipse.org/Use_Case_-_Consuming_ODA_Data_Sources
沒有留言:
張貼留言