现在做个项目需要在Java application 中直接调用crystal report的API ,不用JNDI的方式链接数据库,不用JSP,就是说直接在Java application 的main方法里面去实现crystal report的链接数据库更能,请问有什么办法吗?
以下是我通过JNDI的方法实现的代码,可以正常运行,现在求不用JNDI的方法:
public void runReportUsingSql() throws Exception {
String jndiName ="fams_system";
String reportPath ="C:/MyDevelopTools/FARMS/WebApp/WebContent/reporting/wizard/AT020.RPT";
final String sql="SELECT SYSTEM_TYPE_MSTR.ID_TYPE, SYSTEM_TYPE_MSTR.DESC_TYPE, SYSTEM_TYPE_MSTR.NOTE_TYPE, SYSTEM_TYPE_MSTR.NAME_SUPR, SYSTEM_CLAS_MSTR.ID_CLAS, SYSTEM_CLAS_MSTR.DESC_CLAS, SYSTEM_CLAS_MSTR.NOTE_CLAS, SYSTEM_CLAS_MSTR.ID_PARA, SYSTEM_CLAS_MSTR.CODE_AUTH, SYSTEM_CLAS_MSTR.CODE_XREF FROM FAMSADMN.SYSTEM_TYPE_MSTR SYSTEM_TYPE_MSTR, FAMSADMN.SYSTEM_CLAS_MSTR SYSTEM_CLAS_MSTR WHERE SYSTEM_TYPE_MSTR.ID_TYPE = SYSTEM_CLAS_MSTR.ID_TYPE AND SYSTEM_TYPE_MSTR.ID_TYPE = 'MEMBER' AND SYSTEM_CLAS_MSTR.ID_CLAS IN ('DADADADA') ORDER BY SYSTEM_CLAS_MSTR.ID_CLAS ASC, SYSTEM_TYPE_MSTR.ID_TYPE ASC FOR FETCH ONLY";
reportClientDoc.setReportAppServer(ReportClientDocument.inprocConnectionString);
reportClientDoc.open(reportPath, OpenReportOptions._openAsReadOnly);
ITable table = reportClientDoc.getDatabaseController().getDatabase().getTables().getTable(0);
IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();
// Assign the old Connection info to the reports current info
DatabaseController dbController = reportClientDoc.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);
// Create a new propertybag for the new location
PropertyBag propertyBag = oldConnectionInfo.getAttributes();
propertyBag.put("Use JDBC", true);
propertyBag.put("Use JNDI", true);
propertyBag.put("JNDI Datasource Name", jndiName);
newConnectionInfo.setAttributes(propertyBag);
newConnectionInfo.setKind(ConnectionInfoKind.SQL);
Fields pFields = null;
if (table instanceof CommandTable) {
// ((CommandTable) table).setCommandText(sql);
}
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
reportClientDoc.getDatabaseController().replaceConnection(oldConnectionInfo, newConnectionInfo, pFields,
replaceParams);
try{
String result="C:/MyDevelopTools/ReportResult.pdf";
Fields fields = reportClientDoc.getDataDefController().getDataDefinition().getParameterFields();
for (int i = 0; i < fields.size(); i++) {
IParameterField paramField = (IParameterField)fields.getField(i);
String name = paramField.getName();
reportClientDoc.getDataDefController().getParameterFieldController().setCurrentValue("", name , new String("guojun"));
}
ByteArrayInputStream byteInput = (ByteArrayInputStream) reportClientDoc
.getPrintOutputController().export(ReportExportFormat.PDF);
byte byteArray[] = new byte[byteInput.available()];
File file = new File(result);
FileOutputStream fileOut = new FileOutputStream(file);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(byteInput
.available());
int x = byteInput.read(byteArray, 0, byteInput.available());
byteOut.write(byteArray, 0, x);
byteOut.writeTo(fileOut);
byteOut.close();
fileOut.close();
// reportClientDoc.close();
}
catch (Exception e) {
throw e;
}
}