升级Crystal Report 8.5 to Crystal Report 2011

升级过程中有两点需要注意:

  • 在AIX安装字体,繁体就用新细明体mingliu.ttf和mingliub.ttf(粗体),英文就用Times New Roman:times.ttf,timesbd.ttf,timesbi.ttf,timesi.ttf。
  • 设置Locale,因不知为何在AIX上生成的PDF文件编码总是日文语系的,当设置完Locale后问题才得以解决,reportClientDoc.setLocale(Locale.TRADITIONAL_CHINESE);
  • 运行时动态改变datasource的jndi,在oralce上尝试几个方法都失败,但感觉这些code都应该有参考价值的,也会列出来。

ReportServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package dh.report;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.crystaldecisions.report.web.viewer.CrPrintMode;
import com.crystaldecisions.report.web.viewer.CrystalReportViewer;
import com.crystaldecisions.report.web.viewer.ReportExportControl;
import com.crystaldecisions.sdk.occa.report.application.ISubreportClientDocument;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.Fields;
import com.crystaldecisions.sdk.occa.report.data.ParameterField;
import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue;
import com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.IStrings;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKExceptionBase;
import com.crystaldecisions.sdk.occa.report.reportsource.IReportSource;

public class ReportServlet extends HttpServlet {

public static String JNDI_NAME = "java:comp/env/jdbc/psuat";

//public static String JNDI_NAME = "jdbc/dhprd";

@Override
public void init(ServletConfig config) throws ServletException {
String fontsPath = config.getServletContext().getRealPath("/") + "/WEB-INF/lib/fonts/";
System.out.println("real path " + fontsPath);
try {
/*GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "mingliu.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "mingliub.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "times.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "timesbd.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "timesbi.ttf")));
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(fontsPath + "timesi.ttf")));

String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

for (int i = 0; i < fonts.length; i++) {
System.out.println("System font " + fonts[i]);
}*/

} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String report = req.getParameter("report");
if (report != null) {
report = report.replace("file:", "");

if ("127.0.0.1".equals(req.getLocalAddr()) || "10.13.135.40".equals(req.getLocalAddr())) {
report = report.replace("/piers/apps/uat/installedApps/DHApps.ear/dh.war/rpt", "");
} else {
if (report.indexOf("uat") != -1) {
report = report.replace("/piers/apps/uat", "/dh/apps/prod");
}
}
}

// List<String> promptValues = retrieveParameterValues(req);

System.out.println("report = " + report);

ReportClientDocument reportClientDoc = new ReportClientDocument();

Map parameterMap = new HashMap();

try {
reportClientDoc.setLocale(Locale.TRADITIONAL_CHINESE);

reportClientDoc.open(report, 0);

ReportUtil.switch_tables(reportClientDoc.getDatabaseController());

//Perform the same operation against all tables in the subreport as well.
IStrings subreportNames = reportClientDoc.getSubreportController().getSubreportNames();

//Set the datasource for all the subreports.
for (int i = 0; i < subreportNames.size(); i++) {
ISubreportClientDocument subreportClientDoc = reportClientDoc.getSubreportController().getSubreport(
subreportNames.getString(i));

//Switch tables for each subreport in the report using the same connection information. See utility
//method below.
ReportUtil.switch_tables(subreportClientDoc.getDatabaseController());

}

Fields paraFields = reportClientDoc.getDataDefinition().getParameterFields();

for (int i = 0; i < paraFields.size(); i++) {
String value = "";

// get pre-set value
ParameterField field = (ParameterField) paraFields.get(i);
if (field.getValues() != null && field.getValues().size() > 0) {
ParameterFieldDiscreteValue v = (ParameterFieldDiscreteValue) field.getValues().get(0);
value = v.getValue().toString();
}

String paramValue = req.getParameter("prompt" + i);
System.out.println("paramValue = " + paramValue);
if (paramValue != null) {
value = paramValue;
if (value.indexOf("Date") != -1) {
parameterMap.put(field.getName(), getDate(value));
} else {
parameterMap.put(field.getName(), value);
}
} else {
parameterMap.put(field.getName(), value);
}
}

Fields fields = ReportUtil.prepareParameter(report, parameterMap);

IReportSource reportSource = reportClientDoc.getReportSource();

showByPdf(reportSource, fields, req, resp);
} catch (ReportSDKException e) {
e.printStackTrace();
} catch (ReportSDKExceptionBase e) {
e.printStackTrace();
}

}

private void showByPdf(IReportSource reportSource, Fields fields, HttpServletRequest req, HttpServletResponse resp)
throws ReportSDKExceptionBase {
ExportOptions exportOptions = new ExportOptions();
exportOptions.setExportFormatType(ReportExportFormat.PDF);

ReportExportControl exportControl = new ReportExportControl();
exportControl.setReportSource(reportSource);
exportControl.setExportOptions(exportOptions);
exportControl.setExportAsAttachment(false);
exportControl.setParameterFields(fields);
exportControl.setOwnPage(true);

exportControl.processHttpRequest(req, resp, req.getSession().getServletContext(), null);

exportControl.dispose();
}

private void showByHtml(IReportSource reportSource, Fields fields, HttpServletRequest req, HttpServletResponse resp)
throws ReportSDKExceptionBase, IOException {
//JPEReportSourceFactory rptSrcFactory = new JPEReportSourceFactory();
//Object reportSource =rptSrcFactory.createReportSource(report,Locale.TRADITIONAL_CHINESE);
CrystalReportViewer viewer = new CrystalReportViewer();

viewer.setReportSource(reportSource);
viewer.setPrintMode(CrPrintMode.ACTIVEX);
viewer.setParameterFields(fields);
viewer.setOwnPage(true);
String html = viewer.getHtmlContent(req, resp, getServletContext());
System.out.println("html = " + html);

resp.setHeader("Cache-Control", "no-cache, post-check=0, pre-check=0");
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");

viewer.processHttpRequest(req, resp, req.getSession().getServletContext(), resp.getWriter());
viewer.dispose();
}

private List<String> retrieveParameterValues(HttpServletRequest req) {
List<String> parameterValues = new ArrayList<String>();
Enumeration<String> e = req.getParameterNames();
List<String> paraNames = new ArrayList<String>();

while (e.hasMoreElements()) {
String paramName = e.nextElement();
paraNames.add(paramName);
}

Collections.sort(paraNames);
for (String paramName : paraNames) {
String paramValue = req.getParameter(paramName);

System.out.println("paramName = " + paramName + ", paramValue = " + paramValue);
if (paramName.startsWith("prompt")) {
System.out.println("prompt = " + paramValue);
parameterValues.add(paramValue);
}
}

return parameterValues;
}

private Date getDate(String date) {
Calendar c = Calendar.getInstance();
Pattern p = Pattern.compile("Date\\((\\d+),(\\d+),(\\d+)\\)");
Matcher m = p.matcher(date);
if (m.matches()) {
System.out.println("year " + m.group(1) + ",month " + m.group(2) + ", date " + m.group(3));
c.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
c.set(Calendar.MONTH, Integer.parseInt(m.group(2)));
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(m.group(3)));
}
return c.getTime();
}
}

ReportUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package dh.report;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.crystaldecisions.report.web.viewer.ReportExportControl;
import com.crystaldecisions.sdk.occa.report.application.DBOptions;
import com.crystaldecisions.sdk.occa.report.application.DatabaseController;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfoKind;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfos;
import com.crystaldecisions.sdk.occa.report.data.Fields;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ITable;
import com.crystaldecisions.sdk.occa.report.data.ParameterField;
import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue;
import com.crystaldecisions.sdk.occa.report.data.Tables;
import com.crystaldecisions.sdk.occa.report.data.Values;
import com.crystaldecisions.sdk.occa.report.lib.PropertyBag;
import com.crystaldecisions.sdk.occa.report.lib.PropertyBagHelper;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;

public class ReportUtil {

public static Object getReportSource(HttpSession session, String reportName) throws ReportSDKException {
System.out.println("getReportSource() ---------- start ----------------- \n reportName=" + reportName
+ "\n session=" + session);
// if(session==null || reportName==null){
// return null ;
// }

Object reportSource = session.getAttribute("reportSource");

if (reportSource == null) {
System.out.println("getReportSource() ......... reportSource == null , create reportSource");
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open(reportName, 0);
reportSource = reportClientDoc.getReportSource();
session.setAttribute("reportSource", reportSource);
}
System.out.println("getReportSource() ---------- finish ----------------- \n reportSource=" + reportSource);
return reportSource;

}

public static ParameterField createParameter(String parameterName, Object parameterValue) {
System.out.println("createParameter() ---------- start ----------------- \t parameterName=" + parameterName
+ "\t parameterValue=" + parameterValue);
ParameterField pfield = new ParameterField();
pfield.setName(parameterName);
pfield.setReportName("");

Values vals = new Values();
ParameterFieldDiscreteValue pfieldDV = new ParameterFieldDiscreteValue();
pfieldDV.setValue(parameterValue);
vals.add(pfieldDV);

pfield.setCurrentValues(vals);

return pfield;
}

public static Fields prepareParameter(String reportTemplateFileName, Map parameterMap) {
// logger.info("prepareParameter() ---------- start ----------------- \t parameterName=" + parameterName + "\t parameterValue=" + parameterValue) ;
System.out.println("prepareParameter() ---------- start ----------------- \t reportTemplateFileName="
+ reportTemplateFileName + "\t parameterMap=" + parameterMap);

Fields fields = null;

if (parameterMap != null) {
fields = new Fields();
for (Iterator itr = parameterMap.keySet().iterator(); itr.hasNext();) {
String key = (String) itr.next();
if (key != null) {
Object value = parameterMap.get(key);
fields.add(ReportUtil.createParameter(key, value));
System.out.println("put parameter " + key + " : " + value);
}
}
}

System.out.println("prepareParameter() ---------- Finish ----------------- \t fields=" + fields);

return fields;
}

/*
// ref: Crystal report official tutorial
public static void changeDataSource(ReportClientDocument rcd) throws ReportSDKException
{

System.out.println("changeDataSource() ---------- start ----------------- \t rcd=" + rcd);
DatabaseController databaseController = rcd.getDatabaseController();
ConnectionInfos connectionInfos = (ConnectionInfos)databaseController.getConnectionInfos(null);
IConnectionInfo oldConnectionInfo = connectionInfos.getConnectionInfo(0);
IConnectionInfo newConnectionInfo = new ConnectionInfo();

newConnectionInfo.setKind(ConnectionInfoKind.SQL);

PropertyBag newProperties = new PropertyBag();
newProperties.putStringValue("DSN", "Xtreme Sample Database 2005");
newProperties.put("UseDSNProperties", false);
newProperties.putStringValue(PropertyBagHelper.CONNINFO_SERVER_NAME, "Xtreme Sample Database 2005");
newProperties.putStringValue(PropertyBagHelper.CONNINFO_SERVER_TYPE, "ODBC (RDO)");
newProperties.putStringValue(PropertyBagHelper.CONNINFO_DATABASE_DLL, "crdb_odbc.dll");

newConnectionInfo.setAttributes(newProperties);
newConnectionInfo.setUserName("guest");
newConnectionInfo.setPassword("bobobo");

try
{
databaseController.replaceConnection(oldConnectionInfo, newConnectionInfo, null, DBOptions._useDefault);
}
catch (ReportSDKException error)
{
System.out.println(error.getSDKError());
}

System.out.println("changeDataSource() ---------- finish ----------------- ");
}
*/

// Change the document's all table's source to a JNDI, 实际上是不行的
// ref: http://scn.sap.com/thread/1688419
public static void changeDB(ReportClientDocument rcd, String reportJndiName) {
System.out.println("changeDB() ---------- start ----------------- \t rcd=" + rcd + "\t change to JNDI:"
+ reportJndiName);
DatabaseController databaseController;
try {
databaseController = rcd.getDatabaseController();

IConnectionInfo connectionInfo = new ConnectionInfo();
PropertyBag propertyBag = new PropertyBag();
propertyBag.put("JNDI Datasource Name", reportJndiName);
propertyBag.put("Use JDBC", "true");
// by Ricky
// propertyBag.put("Database DLL", DATABASE_DLL); // required!!! (but we don't actually provide a DLL, or even run in windows)
propertyBag.put(PropertyBagHelper.CONNINFO_DATABASE_DLL, "crdb_oracle.dll");

connectionInfo.setAttributes(propertyBag);
connectionInfo.setKind(ConnectionInfoKind.SQL);

ConnectionInfos connectionInfos = databaseController.getConnectionInfos(null);
IConnectionInfo oldConnectionInfo = connectionInfos.getConnectionInfo(0);

databaseController.replaceConnection(oldConnectionInfo, connectionInfo, null, DBOptions._useDefault);
} catch (ReportSDKException e) {
e.printStackTrace();
}

System.out.println("changeDB() ---------- finish ----------------- ");
}

// 全局性转换登录信息
public static void changeDB2(ReportClientDocument rcd, String reportJndiName) {
String DBUSERNAME = "dhdba";
String DBPASSWORD = "password";
String CONNECTION_STRING = "jdbc:oracle:thin:@server:1521:sid";
String SERVERTYPE = "JDBC (JNDI)";
String DATABASE_DLL = "crdb_oracle.dll";
String DATABASE = "server";
final String TRUSTEDCON = "false";
String DBCLASSNAME = "oracle.jdbc.driver.OracleDriver";
String USEJDBC = "true";
String SERVERNAME = "jdbc:oracle:thin:@server:1521:sid";
String CONNECTIONURL = "jdbc:oracle:thin:@server:1521:sid";
String SERVER = "server";
final String DATABASE_NAME = "sid";

try {
System.out.println("change to " + reportJndiName);
// Set DB Username and Password
//rcd.getDatabaseController().logon(DBUSERNAME, DBPASSWORD);

// Create the two connectioninfo objects to use
IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();

// Assign the old Connection info to the reports current info
DatabaseController dbController = rcd.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);

// Create a new propertybag for the new location
PropertyBag boPropertyBag1 = new PropertyBag();

// Set new table logon properties
boPropertyBag1.put("JDBC Connection String", CONNECTION_STRING);
boPropertyBag1.put("Trusted_Connection", TRUSTEDCON);
boPropertyBag1.put("Server Type", SERVERTYPE);
boPropertyBag1.put("Database DLL", DATABASE_DLL);
boPropertyBag1.put("Database", DATABASE);
boPropertyBag1.put("Database Class Name", DBCLASSNAME);
boPropertyBag1.put("Use JDBC", USEJDBC);
boPropertyBag1.put("Database Name", DATABASE_NAME);
boPropertyBag1.put("Server Name", SERVERNAME);
boPropertyBag1.put("Connection URL", CONNECTIONURL);
boPropertyBag1.put("Server", SERVER);

// Assign the properties to the connection info
newConnectionInfo.setAttributes(boPropertyBag1);

// Set the DB Username and Pwd
newConnectionInfo.setUserName(DBUSERNAME);
newConnectionInfo.setPassword(DBPASSWORD);

// The Kind of connectionInfos is SQL
newConnectionInfo.setKind(ConnectionInfoKind.SQL);

// set the parameters to replace.
// The 4 options are:
// _doNotVerifyDB
// _ignoreCurrentTableQualifiers
// _mapFieldByRowsetPosition
// _useDefault
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
//int replaceParams = DBOptions._useDefault;

// Now replace the connections
dbController.replaceConnection(oldConnectionInfo, newConnectionInfo, null, replaceParams);

//Store the report source in session, will be used by the CrystalReportViewer.

} catch (ReportSDKException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 对Report里每一Table进行转换
public static void changeDB3(ReportClientDocument rcd, String reportJndiName) {
final String TABLE_NAME_QUALIFIER = "dhdba.";
final String DBUSERNAME = "dhdba";
final String DBPASSWORD = "password";
String CONNECTION_STRING = "jdbc:oracle:thin:@server:1521:sid";
final String DATABASE_NAME = "dhdba";
final String URI = "!oracle.jdbc.driver.OracleDriver!jdbc:oracle:thin:@server:1521:sid";
String SERVERNAME = "jdbc:oracle:thin:@server:1521:sid";
final String DATABASE_DLL = "crdb_jdbc.dll";
String DBCLASSNAME = "oracle.jdbc.driver.OracleDriver";

try {
DatabaseController dbController = rcd.getDatabaseController();
Tables tables = dbController.getDatabase().getTables();
for (int i = 0; i < tables.size(); i++) {

ITable table = tables.getTable(i);

//Keep existing name and alias.
table.setName(table.getName());
table.setAlias(table.getAlias());
System.out.println("getName " + table.getName());
System.out.println("getAlias" + table.getAlias());
//Change properties that are different from the original datasource.
table.setQualifiedName(TABLE_NAME_QUALIFIER + table.getName());

//Change connection information properties.
IConnectionInfo connectionInfo = table.getConnectionInfo();
connectionInfo.getAttributes().clear();
//Set new table connection property attributes.
PropertyBag propertyBag = new PropertyBag();

//Overwrite any existing properties with updated values.
propertyBag.put("Trusted_Connection", "false");
propertyBag.put("Server Name", SERVERNAME); //Optional property.
propertyBag.put("Connection String", CONNECTION_STRING);
propertyBag.put("Database Name", DATABASE_NAME);
//propertyBag.put("Server Type", "JDBC (JNDI)");
propertyBag.put("URI", URI);
propertyBag.put("Use JDBC", "false");
propertyBag.put("JDBC Connection String", CONNECTION_STRING);
propertyBag.put("Database Class Name", DBCLASSNAME);
propertyBag.put("Database DLL", DATABASE_DLL);
propertyBag.put("Connection Name", CONNECTION_STRING);
propertyBag.put("PreQEServerName", "server");
propertyBag.put("PASSWORD", "password");

connectionInfo.setAttributes(propertyBag);

//Set database username and password.
//NOTE: Even if these the username and password properties don't change when switching databases, the
//database password is *not* saved in the report and must be set at runtime if the database is secured.
connectionInfo.setUserName(DBUSERNAME);
connectionInfo.setPassword(DBPASSWORD);
connectionInfo.setKind(ConnectionInfoKind.SQL);

table.setConnectionInfo(connectionInfo);
rcd.getDatabaseController().logon(DBUSERNAME, DBPASSWORD);
//Update old table in the report with the new table.
dbController.setTableLocation(table, tables.getTable(i));

}

} catch (ReportSDKException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void changeDB4(ReportClientDocument rcd, String reportJndiName, ReportExportControl exportControl)
throws ReportSDKException {
IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();

String DBUSERNAME = "dhdba";
String DBPASSWORD = "password";
String CONNECTION_STRING = "jdbc:oracle:thin:@server:1521:sid";
String SERVERTYPE = "JDBC (JNDI)";
String DATABASE_DLL = "crdb_oracle.dll";
String DATABASE = "server";
final String TRUSTEDCON = "false";
String DBCLASSNAME = "oracle.jdbc.driver.OracleDriver";
String USEJDBC = "true";
String SERVERNAME = "jdbc:oracle:thin:@server:1521:sid";
String CONNECTIONURL = "jdbc:oracle:thin:@server:1521:sid";
String SERVER = "server";
final String DATABASE_NAME = "sid";

// Assign the old Connection info to the reports current info
DatabaseController dbController = rcd.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);

// Create a new propertybag for the new location
PropertyBag boPropertyBag1 = new PropertyBag();

// Set new table logon properties
boPropertyBag1.put("JDBC Connection String", CONNECTION_STRING);
boPropertyBag1.put("Trusted_Connection", TRUSTEDCON);
boPropertyBag1.put("Server Type", SERVERTYPE);
boPropertyBag1.put("Database DLL", DATABASE_DLL);
boPropertyBag1.put("Database", DATABASE);
boPropertyBag1.put("Database Class Name", DBCLASSNAME);
boPropertyBag1.put("Use JDBC", USEJDBC);
boPropertyBag1.put("Database Name", DATABASE_NAME);
boPropertyBag1.put("Server Name", SERVERNAME);
boPropertyBag1.put("Connection URL", CONNECTIONURL);
boPropertyBag1.put("Server", SERVER);

// Assign the properties to the connection info
newConnectionInfo.setAttributes(boPropertyBag1);

// Set the DB Username and Pwd
newConnectionInfo.setUserName(DBUSERNAME);
newConnectionInfo.setPassword(DBPASSWORD);

// The Kind of connectionInfos is SQL
newConnectionInfo.setKind(ConnectionInfoKind.SQL);

// set the parameters to replace.
// The 4 options are:
// _doNotVerifyDB
// _ignoreCurrentTableQualifiers
// _mapFieldByRowsetPosition
// _useDefault
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;

ConnectionInfos infos = new ConnectionInfos();
infos.add(newConnectionInfo);
Iterator it = infos.iterator();
while (it.hasNext()) {
ConnectionInfo info = (ConnectionInfo) it.next();
PropertyBag bag = info.getAttributes();
System.out.println("EEEEE " + bag.getStringValue("JDBC Connection String"));
System.out.println("EEEEE " + info.getPassword());
}

exportControl.setDatabaseLogonInfos(infos); // 尝试用exportControl设置登录信息
}

// 唯有这个方法可行,其他都失败了
public static void switch_tables(DatabaseController databaseController) throws ReportSDKException {

final String TABLE_NAME_QUALIFIER = "DHDBA.";
final String SERVERNAME = "server";
final String CONNECTION_STRING = "Use JDBC=b(true);Connection URL=s(jdbc:oracle:thin:@server:1521:sid);"
+ "Database Class Name=s(oracle.jdbc.driver.OracleDriver);Server=s(server);"
+ "User ID=s(dhdba);Password=;Trusted_Connection=b(false);"
+ "JDBC Connection String=s(!oracle.jdbc.driver.OracleDriver!jdbc:oracle:thin:{userid}/{password}@server:1521:sid)";
final String JNDI_DATASOURCE_NAME = "testing";
final String DATABASE_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
final String DATABASE_DLL = "crdb_jdbc.dll";
final String DBURI = "!oracle.jdbc.driver.OracleDriver!jdbc:oracle:thin:{userid}/{password}@server:1521:sid";
final String DBUSERNAME = "dhdba";
final String DBPASSWORD = "password";

//Obtain collection of tables from this database controller.
Tables tables = databaseController.getDatabase().getTables();

//Set the datasource for all main report tables.
for (int i = 0; i < tables.size(); i++) {

ITable table = tables.getTable(i);

//Keep existing name and alias.
table.setName(table.getName());
table.setAlias(table.getAlias());

//Change properties that are different from the original datasource.
table.setQualifiedName(TABLE_NAME_QUALIFIER + table.getName());

//Change connection information properties.
IConnectionInfo connectionInfo = table.getConnectionInfo();

PropertyBag innerProp = connectionInfo.getAttributes();
innerProp.clear();

//Set new table connection property attributes.
PropertyBag propertyBag = new PropertyBag();

//Overwrite any existing properties with updated values.
propertyBag.put("Trusted_Connection", "b(false)");
propertyBag.put("Server Name", SERVERNAME);
propertyBag.put("Connection String", CONNECTION_STRING);
propertyBag.put("Server Type", "JDBC (JNDI)");
propertyBag.put("JNDI Datasource Name", JNDI_DATASOURCE_NAME);
propertyBag.put("Database Class Name", DATABASE_CLASS_NAME);
propertyBag.put("Use JDBC", "true");
propertyBag.put("URI", DBURI);
propertyBag.put("Database DLL", DATABASE_DLL);

connectionInfo.setAttributes(propertyBag);

//Set database username and password.
//NOTE: Even if these the username and password properties don't change when switching databases, the
//database password is *not* saved in the report and must be set at runtime if the database is secured.
connectionInfo.setUserName(DBUSERNAME);
connectionInfo.setPassword(DBPASSWORD);
connectionInfo.setKind(ConnectionInfoKind.SQL);

table.setConnectionInfo(connectionInfo);

//Update old table in the report with the new table.
databaseController.setTableLocation(table, tables.getTable(i));

}

}

}

参考资料:
http://www.sdn.sap.com/irj/boc/index?rid=/webcontent/uuid/909355da-5b84-2b10-76a4-eed3ccf9e650
http://www.sdn.sap.com/irj/boc/index?rid=/webcontent/uuid/c020cd81-5c84-2b10-3585-bff096303cf6