【问题标题】:How to open crystal report in java swing application?如何在 java swing 应用程序中打开水晶报表?
【发布时间】:2012-05-21 03:23:25
【问题描述】:

我有这个代码

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));

我要做的是打开一个报表并将连接信息传递给该报表,以便我可以动态更改报表的数据库,但由于某种原因它无法正常工作,并且报表仍会从数据库中生成信息它最初是建立的。有人可以告诉我我做错了什么吗?这是一个摇摆应用程序,我正在使用水晶报表 XI。顺便说一句,我使用 com.crystaldecisions.reports.sdk.ReportClientDocument 而不是 com.crystaldecisions.sdk.occa.report.application.ReportClientDocument 因为当我使用另一个时,我得到一个找不到服务器错误。请帮忙。

【问题讨论】:

  • 我看不出 Swing 是如何在其中真正发挥作用的,除非您可能需要注意在后台线程中调用此代码,否则创建报告的方式与从来自控制台程序的 Swing GUI。
  • 我真的不明白。对不起。一切都很好,除了数据库部分的动态变化。

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


【解决方案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){
    ...
    }

在上面的代码 sn-p 中传递适当的值。抱歉,这使用了 com.crystaldecisions.sdk.occa.report API。

希望这会有所帮助...

【讨论】:

  • 感谢您的回答。 oldConnInfo 和 newConnInfo 都是 java.sql.Connection 对象吗?
  • 我是否需要像我所做的那样更改所有表的属性?如果没有,我如何从 reportclientdocument 中获取 IConnectionInfo?
  • 你可以使用这个获取旧的连接对象:IConnectionInfo oldConnInfo=dbController.getConnectionInfos(null).getConnectionInfo(0);还为新连接创建另一个 IConnectionInfo 对象并为各种属性分配值。我将编辑上述答案以包含这些详细信息。
  • 感谢您的帮助。我试过这个,但我有 servernotfound 错误。这可能是因为我没有水晶报表服务器。我的电脑中只安装了标准的水晶报表设计器。 :(
  • 我不认为它与 Crystal Reports Server 有任何关系...因为我也只有 CR 设计师,它对我很有效...
【解决方案2】:

水晶报表开发可以使用eclipse版here。您可以从那里下载 eclipse 的插件。

您可以找到一个很好的例子来开始使用 Java here 进行水晶报表开发

您可以找到与水晶报告相关的答案here,其中列出了有关使用 Java 开发水晶报告的所有必要信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多