java - 如何在java swing应用程序中打开 Crystal 报表?

标签 java swing crystal-reports crystal-reports-xi

我有这个代码

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,因为当我使用另一个时,出现找不到服务器错误。请帮忙。

最佳答案

要在运行时切换连接,你可以使用这个:

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。

希望这有助于...

关于java - 如何在java swing应用程序中打开 Crystal 报表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10679179/

相关文章:

java - ImageView 未出现在抽屉导航中的 ListView 下方

java - JAXB 创建空对象

java - JComponent InputMap 神秘地被删除

java - 不尊重插图

java - 在 Java GUI 中绘制图像

c# - 在 C# Windows 应用程序中增加 Crystal Reports 的宽度

.net - 在 Crystal Reports 中动态更改 XSD 文件路径

C# 以编程方式创建报告

java - OpenCV android VideoWriter 问题

Java DB 客户端-服务器技术 - 集中式 DB - 如何?