cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic xml datasource in Java (CR4E)

Former Member
0 Kudos

Dear all,

Is there any simple sample to show how to display rpt via dynamic XML datasource during runtime.

This is the current environment: jdk 1.6, crystal report xi, tomcat 5.5. Now I could new crystal report using XML datasouce when development. what I what is to modify the path of xml file in the JSP page.


I did a lot of search but with no luck. Result shows a lot of examples for database connection modification in runtime.


Best Regards

Rodney Huang

Accepted Solutions (1)

Accepted Solutions (1)

former_member183750
Active Contributor
0 Kudos

HI Rodney

I don't deal with Java at all, but maybe these sample apps will help:

Java JRC Set Resultset Datasource | SCN

CRJava Result Set Data Source | SCN

CRJava POJO Data Source | SCN

If not, I'll see about chasing someone down that will be able to help.

- Ludek

Senior Support Engineer AGS Product Support, Global Support Center Canada

Former Member
0 Kudos

Dear Ludek,

Many thanks for your helpful reply and it makes my mind much more clear. Now my problem goes clear as "How to change XML connection property in runtime".

Just found one similar question as this at below address:

http://scn.sap.com/thread/1247885

In above topic, Ted used to said below:

"You can switch XML datasource by changing the connection info properties before running the document.   So if the report remains unchanged but you just want a different datasource for the report, it'll be just doing replaceConnection as you would with any other data source type".

May I know where to find the JSP sample to show the code snippet about "switch XML datasource"?

Best Regards,

Rodney

former_member183750
Active Contributor
0 Kudos

Hi Rodney

Unfortunately, the only samples available are the ones linked to in my previous reply. However, I may be able to dog out samples in .NET. Not sure if that would help. E.g.; you'd have to translate to Java.

- Ludek

Former Member
0 Kudos

Dear Ludek,

It is pity that no availalbe sample.

Look forward to see your samples in .NET to seek for instruction.

Many thanks.

Best Regards,

Rodney

Former Member
0 Kudos

Dear Lude,

Finally I make it happen with below code:

* The basic idea is to updatethe conneciton information in runtime.

<%@ page import="com.crystaldecisions.report.web.viewer.CrystalReportViewer,
                 java.io.ByteArrayInputStream,
                 java.sql.Connection,
                 java.sql.DriverManager,
                 java.sql.ResultSet,
                 java.sql.Statement,
                 com.crystaldecisions.sdk.occa.report.data.*,
                 com.crystaldecisions.sdk.occa.report.application.DatabaseController,
                 com.crystaldecisions.sdk.occa.report.application.DBOptions,
                 com.crystaldecisions.sdk.occa.report.application.OpenReportOptions,
                 com.crystaldecisions.sdk.occa.report.application.ReportClientDocument,                
                 com.crystaldecisions.sdk.occa.report.lib.*,
                 com.crystaldecisions.sdk.occa.report.exportoptions.*;                                
                 "
%><%
String reportPath;
ReportClientDocument reportClientDocument;
Connection connection;
Statement statement;
ResultSet resultSet;
ByteArrayInputStream byteArrayInputStream;
byte[] byteArray;
int bytesRead;

reportPath = "TEST/po.rpt";

final String LOCAL_SCHEMA_FILE = "D:\\po.xsd";
final String SERVER_TYPE       = "XML";

final String PREQESERVERNAME   = "D:\\po.xml D:\\po.xsd";
final String DATABASE_DLL      = "crdb_xml.dll";
final String PREQESERVERTYPE   = "XML";
final String SERVER_NAME       = "D:\\po.xml D:\\po.xsd";
final String LOCAL_XML_FILE    = "D:\\po.xml";

reportClientDocument = new ReportClientDocument();
reportClientDocument.open(reportPath, 0);

IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();

DatabaseController dbController = reportClientDocument.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0); 

PropertyBag boPropertyBag1 = new PropertyBag();

boPropertyBag1.put("Database DLL",      DATABASE_DLL);   
boPropertyBag1.put("PreQEServerName",   PREQESERVERNAME);
boPropertyBag1.put("PreQEServerType",   PREQESERVERTYPE);
boPropertyBag1.put("Server Type",       SERVER_TYPE);
boPropertyBag1.put("Server Name",       SERVER_NAME);
boPropertyBag1.put("Local XML File",    LOCAL_XML_FILE);
boPropertyBag1.put("Local Schema File", LOCAL_SCHEMA_FILE);

newConnectionInfo.setAttributes(boPropertyBag1);

int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;

dbController.replaceConnection(oldConnectionInfo, newConnectionInfo, null, replaceParams);
session.setAttribute("reportSource", reportClientDocument.getReportSource());

byteArrayInputStream = (ByteArrayInputStream) reportClientDocument
        .getPrintOutputController().export(ReportExportFormat.PDF);

response.reset();

response.setHeader("Content-disposition", "inline;filename=crreport.pdf");
response.setContentType("application/pdf");

byteArray = new byte[1024];
while((bytesRead = byteArrayInputStream.read(byteArray)) != -1) {
response.getOutputStream().write(byteArray, 0, bytesRead);
}

response.getOutputStream().flush();
response.getOutputStream().close();

reportClientDocument.close();
%>

former_member183750
Active Contributor
0 Kudos

Hi Rodney

Many, many thanx for sharing the code. Yesterday I was looking for one person that would probably have the code, but I was not able to connect with him. I am very sure your code will come in handy for a lot of people following in your footsteps though

Have a great day,

- Ludek

Former Member
0 Kudos

Dear Ludek,

Just share one import note: in web project, we need to copy XMLConnector.jar into bin folder of TOMCAT. Otherwise we will meet exception about database connection loading which is another common question in the community.

* XMLConnector.jar is included in crystal report java runtime.

Have a nice day.

Best Regards,

- Rodney

Answers (1)

Answers (1)

0 Kudos

Moved to Java SDK forum...