如何使用java swing在Crystal Reports中运行时切换数据库连接

时间:2012-05-21 10:07:04

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

请帮忙。我正在开发一个可以打开使用水晶报告xi制作的报告的swing应用程序。我想要做的是打开一个报告并将连接信息传递给该报告,以便我可以动态更改报告的数据库。但由于某种原因,它无法正常工作,报告仍然会从最初设置的数据库中生成信息。有人可以告诉我我做错了什么吗?顺便说一句,当我尝试使用com.crystaldecisions.sdk.occa.report.application.ReportClientDocument时,我收到服务器未找到错误。但是当我尝试使用import com.crystaldecisions.reports.sdk.ReportClientDocument;它不起作用。我不确定报告服务器是什么,我不认为我在我的计算机上设置它。请帮忙。这是我的代码:

import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;

ReportClientDocument rpt =  new ReportClientDocument();
    rpt.open(reportPath+fileName, 0);
    Fields fields = null;

    IConnectionInfo connInfo = rpt.getDatabaseController().getConnectionInfos(null).getConnectionInfo(0);

     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("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(innerProp);
        connInfo.setPassword(DBConnect.getPassword());
        connInfo.setUserName(DBConnect.getUsername());




    int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
    rpt.getDatabaseController().replaceConnection(rpt.getDatabaseController().getConnectionInfos(null).getConnectionInfo(0), connInfo, fields, replaceParams);

1 个答案:

答案 0 :(得分:1)

您没有使用添加到propertyBag的属性。您应该将属性添加到innerProp,将propertyBag.put(...)替换为innerProp.put(...)或将connInfo.setAttributes(innerProp)更改为connInfo.setAttributes(propertyBag)。我建议采用第一种方法。