如何在Java Swing应用程序中打开Crystal Report?

6
我有这段代码
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));

我想做的是打开一个报告并传递连接信息到报告中,以便我可以动态更改报告的数据库,但出于某种原因它没有工作,报告仍然生成最初设置的数据库中的信息。请问有人能告诉我我做错了什么吗?这是一个swing应用程序,我正在使用Crystal Reports XI。顺便说一下,我正在使用com.crystaldecisions.reports.sdk.ReportClientDocument而不是com.crystaldecisions.sdk.occa.report.application.ReportClientDocument,因为当我使用另一个时,我会收到一个无法找到服务器错误。请帮忙。

1
我不认为Swing在这方面有任何作用,除非你需要注意在后台线程中调用此代码,但是从Swing GUI或控制台程序创建报告的方式是相同的。 - Hovercraft Full Of Eels
我真的没听懂。对不起。除了动态更改数据库部分外,一切都很好。 - John
2个回答

1

在运行时切换连接,您可以使用以下方法:

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){
    ...
    }

请在上述代码片段中传递适当的值。很抱歉,这需要使用com.crystaldecisions.sdk.occa.report API。
希望这可以帮到您...

谢谢您的回答。oldConnInfo和newConnInfo都是java.sql.Connection对象吗? - John
我需要像之前一样更改所有表格的属性吗?如果不需要,我应该如何从reportclientdocument获取IConnectionInfo? - John
你可以使用以下代码获取旧的连接对象:IConnectionInfo oldConnInfo=dbController.getConnectionInfos(null).getConnectionInfo(0); 同时,创建另一个IConnectionInfo对象用于新的连接,并为各个属性分配值。我将编辑上面的答案以包含这些细节。 - ria
谢谢你的帮助。我尝试了这个,但是出现了“服务器未找到”错误。这可能是因为我没有Crystal Reports服务器,我只在我的电脑上安装了标准的Crystal Report Designer。:( - John
我认为这与Crystal Reports Server无关...因为我只有CR设计师,它对我很有效... - ria

0

您可以使用Eclipse版本进行Crystal Report开发here。您可以从这里下载Eclipse插件。

您可以在此处找到一个很好的示例,以开始使用Java here开发Crystal Reports。

您可以在here找到与Crystel报告相关的答案,其中包含有关使用Java进行Crystal报告开发的所有必要信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接