CommandTable Cystal报告错误

时间:2016-03-24 14:44:38

标签: java crystal-reports

我有一个使用复杂SQL编写的Crystal Report,我尝试使用Crystal Report Java API调用它。此报告具有与之关联的Command对象。

  1. 我加载报告并设置连接参数。
  2. 然后我尝试将Connection信息设置为当前的JDBC Profile。含义测试环境凭据。
  3. 我得到一个例外。我尝试使用版本11.版本12。他们似乎都没有工作。

    当我调用下面的代码时,我得到了异常。这段代码可以很好地处理没有" Command" sql语句。

    try{
        clientDoc.getDatabaseController().setTableLocation(
        origTable, newTable);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    

    请参阅下面的整个代码。如果有人知道如何解决这个问题,请回复。

    private static void changeDataSource(ReportClientDocument clientDoc,
            String reportName, String tableName, String username,
            String password, String connectionURL, String driverName,
            String jndiName) throws ReportSDKException {
    
        PropertyBag propertyBag = null;
        IConnectionInfo connectionInfo = null;
        ITable origTable = null;
        ITable newTable = null;
    
        // Declare variables to hold ConnectionInfo values.
        // Below is the list of values required to switch to use a JDBC/JNDI
        // connection
        String TRUSTED_CONNECTION = "false";
        String SERVER_TYPE = "JDBC (JNDI)";
        String USE_JDBC = "true";
        String DATABASE_DLL = "crdb_jdbc.dll";
        String JNDI_OPTIONAL_NAME = jndiName;
        String CONNECTION_URL = connectionURL;
        String DATABASE_CLASS_NAME = driverName;
    
        // Declare variables to hold database User Name and Password values
        String DB_USER_NAME = username;
        String DB_PASSWORD = password;
        System.out.println("Trusted_Connection:" + TRUSTED_CONNECTION);
        System.out.println("Server Type:" + SERVER_TYPE);
        System.out.println("Use JDBC:" + USE_JDBC);
        System.out.println("Database DLL:" + DATABASE_DLL);
        System.out.println("JNDIOptionalName:" + JNDI_OPTIONAL_NAME);
        System.out.println("Connection URL:" + CONNECTION_URL);
        System.out.println("Database Class Name:" + DATABASE_CLASS_NAME);
        System.out.println("DB_USER_NAME:" + DB_USER_NAME);
        System.out.println("DB_PASSWORD:" + DB_PASSWORD);
        // Obtain collection of tables from this database controller
        if (reportName == null || reportName.equals("")) {
            Tables tables = clientDoc.getDatabaseController().getDatabase()
                    .getTables();
            for (int i = 0; i < tables.size(); i++) {
                origTable = tables.getTable(i);
    
                if (tableName == null || origTable.getName().equals(tableName)) {
                    newTable = (ITable) origTable;
                    newTable.setQualifiedName(origTable.getAlias());
                    connectionInfo = newTable.getConnectionInfo();
    
                    // Set new table connection property attributes
                    propertyBag = new PropertyBag();
                    // Overwrite any existing properties with updated values
                    propertyBag.put("Trusted_Connection", TRUSTED_CONNECTION);
                    propertyBag.put("Server Type", SERVER_TYPE);
                    propertyBag.put("Use JDBC", USE_JDBC);
                    propertyBag.put("Database DLL", DATABASE_DLL);
                    propertyBag.put("JNDIOptionalName", JNDI_OPTIONAL_NAME);
                    propertyBag.put("Connection URL", CONNECTION_URL);
                    propertyBag.put("Database Class Name", DATABASE_CLASS_NAME);
                    connectionInfo.setAttributes(propertyBag);
                    connectionInfo.setUserName(DB_USER_NAME);
                    connectionInfo.setPassword(DB_PASSWORD);
    
                    // Update the table information
                    try{
                        clientDoc.getDatabaseController().setTableLocation(
                            origTable, newTable);
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        }
        // Next loop through all the subreports and pass in the same
        // information. You may consider
        // creating a separate method which accepts
        if (reportName == null || !(reportName.equals(""))) {
            IStrings subNames = clientDoc.getSubreportController()
                    .getSubreportNames();
            for (int subNum = 0; subNum < subNames.size(); subNum++) {
                Tables tables = clientDoc.getSubreportController()
                        .getSubreport(subNames.getString(subNum))
                        .getDatabaseController().getDatabase().getTables();
                for (int i = 0; i < tables.size(); i++) {
                    origTable = tables.getTable(i);
                    if (tableName == null
                            || origTable.getName().equals(tableName)) {
                        newTable = (ITable) origTable;
    
                        newTable.setQualifiedName(origTable.getAlias());
                        // Change connection information properties
                        connectionInfo = newTable.getConnectionInfo();
                        // Set new table connection property attributes
                        propertyBag = new PropertyBag();
    
                        // Overwrite any existing properties with updated values
                        propertyBag.put("Trusted_Connection",
                                TRUSTED_CONNECTION);
                        propertyBag.put("Server Type", SERVER_TYPE);
                        propertyBag.put("Use JDBC", USE_JDBC);
                        propertyBag.put("Database DLL", DATABASE_DLL);
                        propertyBag.put("JNDIOptionalName", JNDI_OPTIONAL_NAME);
                        propertyBag.put("Connection URL", CONNECTION_URL);
                        propertyBag.put("Database Class Name",
                                DATABASE_CLASS_NAME);
                        connectionInfo.setAttributes(propertyBag);
                        connectionInfo.setUserName(DB_USER_NAME);
                        connectionInfo.setPassword(DB_PASSWORD);
    
                        // Update the table information
                        clientDoc.getSubreportController()
                                .getSubreport(subNames.getString(subNum))
                                .getDatabaseController()
                                .setTableLocation(origTable, newTable);
                    }
                }
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

在你的这一行后添加

connectionInfo.setPassword(DB_PASSWORD); 
newTable.setConnectionInfo(connectionInfo);
//This will add connection parameters to the new table

而不是

clientDoc.getDatabaseController().setTableLocation(origTable, newTable);

替换它
clientDoc.getDatabaseController ().setTableLocation (newTable,  tables.getTable(i));