6

I have this code

import com.crystaldecisions.reports.sdk.ReportClientDocument;
...

ReportClientDocument rpt =  new ReportClientDocument();
    rpt.open(reportPath+fileName, 0);
    rpt.getDatabaseController().logon(DBConnect.getUsername(), DBConnect.getPassword());
    Tables tables = rpt.getDatabaseController().getDatabase().getTables();

    for(int i=0; i< tables.size(); i++){
        System.out.print(i);
        ITable table = tables.getTable(i);

        IConnectionInfo connInfo = table.getConnectionInfo();

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

        PropertyBag propertyBag = new PropertyBag();
        propertyBag.put("Server Type", "JDBC (JNDI)");
        propertyBag.put("Database DLL", "crdb_jdbc.dll");
        propertyBag.put("Connection String", DBConnect.getConnectionString());
        propertyBag.put("Database Class Name", "com.mysql.jdbc.Driver");
        propertyBag.put("Use JDBC", "true");
        propertyBag.put("Server Name", DBConnect.getServer());
        propertyBag.put("Generic JDBC Driver Behavior", "No");
        propertyBag.put("URI", "!com.mysql.jdbc.Driver!jdbc:mysql://"+DBConnect.getServer()+":"+DBConnect.getPort()+"/"+DBConnect.getDatabase()+"!ServerType=29!QuoteChar=`");

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

        table.setConnectionInfo(connInfo);
        rpt.getDatabaseController().setTableLocation(table, tables.getTable(i));

What Im trying to do is to open a report and pass the connection info to that report so that I can dynamically change the database of the report but for some reason it is not working and the report still produces the information from the database that it was initially set up. Can someone please tell me what I did wrong? This is a swing application and I am using crystal reports XI. Btw Im using com.crystaldecisions.reports.sdk.ReportClientDocument instead of com.crystaldecisions.sdk.occa.report.application.ReportClientDocument because when I use the other one, I get a cannot find server error. Please help.

John
  • 836
  • 2
  • 25
  • 39
  • 1
    I don't see how Swing really plays any role in this except perhaps that you may need to take care to call this code in a background thread, but otherwise creating a report is done the same way from a Swing GUI as from a console program. – Hovercraft Full Of Eels May 21 '12 at 03:27
  • I really didnt get that. Sorry. Everything's fine except for the dynamic changing of database part. – John May 21 '12 at 03:56

2 Answers2

1

To switch connection at runtime you can use this:

IConnectionInfo oldConnInfo = new ConnectionInfo();
IConnectionInfo newConnInfo = new ConnectionInfo();

// If this connection needed parameters, we would use this field.   
com.crystaldecisions.sdk.occa.report.data.Fields pFields = null;

try{
    // Assign the old Connection info to the reports current info
    //DatabaseController dbController = rptClient.getDatabaseController();
    oldConnInfo=dbController.getConnectionInfos(null).getConnectionInfo(0);
    com.crystaldecisions.sdk.occa.report.lib.PropertyBag boPropertyBag1 = new com.crystaldecisions.sdk.occa.report.lib.PropertyBag();
    boPropertyBag1.put("JDBC Connection String","...");
    boPropertyBag1.put("Server Type","...");
    boPropertyBag1.put("Database Type","...");
    boPropertyBag1.put("Database Class Name","...");
    boPropertyBag1.put("Use JDBC","...");
    boPropertyBag1.put("Connection URL","...");
    boPropertyBag1.put("Database DLL","...");
    // Assign the properties to the connection info
    newConnInfo.setAttributes(boPropertyBag1);
    // Set the DB Username and Pwd
    newConnInfo.setUserName("...");
    newConnInfo.setPassword("...");    
    // The Kind of connectionInfos is SQL
    newConnInfo.setKind(ConnectionInfoKind.SQL);

    // set the parameters to replace.
    // The 4 options are:
    // _doNotVerifyDB 
    // _ignoreCurrentTableQualifiers 
    // _mapFieldByRowsetPosition 
    // _useDefault  
    int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
    // Now replace the connections
    dbController.replaceConnection(oldConnInfo, newConnInfo, pFields, replaceParams);
    }catch(ReportSDKException rse){
    ...
    }

Pass appropriate values in the above code snippet. Sorry that this uses com.crystaldecisions.sdk.occa.report APIs.

Hope this helps...

ria
  • 7,904
  • 11
  • 39
  • 46
  • Thank you for the answer. Are oldConnInfo and newConnInfo both java.sql.Connection objects? – John May 21 '12 at 04:41
  • Do I need to change the attributes of all the tables just like what I did? If not, how do I get the IConnectionInfo from the reportclientdocument? – John May 21 '12 at 06:34
  • You can get the old connection object using this: IConnectionInfo oldConnInfo=dbController.getConnectionInfos(null).getConnectionInfo(0); Also create another IConnectionInfo object for the new connection and assign values for the various properties. I'll edit the above answer to include those details. – ria May 21 '12 at 14:13
  • thank you for your help. I tried this but I have the servernotfound error. This might be because I dont have a crystal reports server. I only have the standard crystal report designer installed in my computer. :( – John May 22 '12 at 00:18
  • I dont think it has anything to do with Crystal Reports Server... bcoz I also have only CR designer and it works well for me... – ria May 25 '12 at 08:38
0

You can use eclipse version for crystal report development here. From where you can download the plugins for eclipse.

You can find a good example to start with crystal reports development with Java here

You can find your answer related to crystel reports here , where all the necessary information regarding to crystal reports development with Java is stated.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86