Home
Analytics
Setting JDBC connection string at runtime
drroyalty
I'm using the runtime engine API in my application. I'd like to be able to set the JDBC connection string URL at runtime so that we can use the same rptdesign file for development and production.
Anybody know how to do this? Sample code would be much appreciated.
thanks,
Dan
Find more posts tagged with
Comments
Virgil Dodson
Hi Dan,<br />
<br />
You can use the property binding tab in the DataSet of the BIRT report to set the JDBC connection information dynamically based on a parameter... and then use the REAPI to set the parameter.<br />
<br />
Examples here:<br />
<a class='bbc_url' href='
http://www.birt-exchange.org/devshare/designing-birt-reports/176-changing-a-data-set-039-s-query/#description'>Changing
a data set's query - Designs & Code - BIRT Exchange</a>
drroyalty
Hi Virgil.
Thanks! That worked like a charm.
For anybody else who might be interested, what I did was:
1. Created a report parameter named RP_JdbcDriverUrl. No default value.
2. Set the Data Source's JDBC Driver URL property binding to the expression: reportContext.getParameterValue("RP_JdbcDriverUrl").
3. In my java program, set the value of the report parameter to the desired URL.
Dan
markk
Hi Dan!
Could you give more details on the 3rd item:
"3. In my java program, set the value of the report parameter to the desired URL."
What handler did you extend and which event did you use?
Thank you,
Mark
droyalty
Hi Mark.
Sorry for the slow reply...have not been monitoring this forum.
You have probably figured it out by now, but anybody else here are some code snippets that might be helpful:
Here's my executeReport method; it's in class ReportHelper:
public byte[] executeReport(Report report, Map params, String tmpFilename,
String docType) throws BirtException {
IReportRunnable design = null;
String reportPath = REPORTS_DIR + report.getPath();
IReportEngine engine = getReportEngine();
design = engine.openReportDesign(reportPath);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
IRenderOption options = getRenderOption(docType);
File outFile = new File(m_oUserTempDir, tmpFilename);
String path = outFile.getAbsolutePath();
options.setOutputFileName(path);
task.getAppContext();
task.setRenderOption(options);
task.setParameterValues(params);
task.run();
task.close();
byte[] content = fileToByteArray(outFile);
outFile.delete();
return content;
}
Here's how I call the method above:
ReportHelper rh = getReportHelper();
byte[] content;
try {
content = rh.executeReport(m_oReportToRun, m_oReportParams,
filename, m_oCurrentRecord.getDocType());
}
catch (Exception e) {
throw new RuntimeException(e);
}
Here's how I set the report parameters:
private void setReportParams() {
m_oReportParams.clear();
m_oReportParams.put(PARAM_JDBC_DRIVER_URL, m_sBirtJdbcDriverUrl);
java.sql.Date sqlDate = new java.sql.Date(m_oParamTradeDateStart.getTime());
m_oReportParams.put(PARAM_START_DATE, sqlDate);
sqlDate = new java.sql.Date(m_oParamTradeDateEnd.getTime());
m_oReportParams.put(PARAM_END_DATE, sqlDate);
}
Hope that helps.
Dan