如何将JNDI添加到Crystal报表bean?

时间:2018-05-18 15:44:37

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

所有

我在SAP的页面上找到了一个“ Crystal Report PDF转换器”的示例。它在“简单”报告中工作正常,但是当报告包含数据库连接时,它会失败。这是我得到的:

import com.crystaldecisions.sdk.occa.report.lib.*;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.exportoptions.*;

//Java imports.
import java.io.*;

public class Reader {

//  static final String REPORT_NAME = "C:\\works_fine.rpt";
    static final String REPORT_NAME = "C:\\problematic.rpt";
    static final String EXPORT_FILE = "C:\\myExportedReport.pdf";

    public static void main(String[] args) {
        try {

            // Open report.
            ReportClientDocument reportClientDoc = new ReportClientDocument();
            reportClientDoc.open(REPORT_NAME, 0);

            // NOTE: If parameters or database login credentials are required,
            // they need to be set before.
            // calling the export() method of the PrintOutputController.

            // Export report and obtain an input stream that can be written to
            // disk.
            // See the Java Reporting Component Developer's Guide for more
            // information on the supported export format enumerations
            // possible with the JRC.
            ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) reportClientDoc
                    .getPrintOutputController().export(ReportExportFormat.PDF);

            // Release report.
            reportClientDoc.close();

            // Use the Java I/O libraries to write the exported content to the
            // file system.
            byte byteArray[] = new byte[byteArrayInputStream.available()];

            // Create a new file that will contain the exported result.
            File file = new File(EXPORT_FILE);
            FileOutputStream fileOutputStream = new FileOutputStream(file);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                    byteArrayInputStream.available());
            int x = byteArrayInputStream.read(byteArray, 0,
                    byteArrayInputStream.available());

            byteArrayOutputStream.write(byteArray, 0, x);
            byteArrayOutputStream.writeTo(fileOutputStream);

            // Close streams.
            byteArrayInputStream.close();
            byteArrayOutputStream.close();
            fileOutputStream.close();

            System.out
                    .println("Successfully exported report to " + EXPORT_FILE);

        } catch (ReportSDKException ex) {

            ex.printStackTrace();

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }
}

import com.crystaldecisions.sdk.occa.report.lib.*; import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument; import com.crystaldecisions.sdk.occa.report.exportoptions.*; //Java imports. import java.io.*; public class Reader { // static final String REPORT_NAME = "C:\\works_fine.rpt"; static final String REPORT_NAME = "C:\\problematic.rpt"; static final String EXPORT_FILE = "C:\\myExportedReport.pdf"; public static void main(String[] args) { try { // Open report. ReportClientDocument reportClientDoc = new ReportClientDocument(); reportClientDoc.open(REPORT_NAME, 0); // NOTE: If parameters or database login credentials are required, // they need to be set before. // calling the export() method of the PrintOutputController. // Export report and obtain an input stream that can be written to // disk. // See the Java Reporting Component Developer's Guide for more // information on the supported export format enumerations // possible with the JRC. ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) reportClientDoc .getPrintOutputController().export(ReportExportFormat.PDF); // Release report. reportClientDoc.close(); // Use the Java I/O libraries to write the exported content to the // file system. byte byteArray[] = new byte[byteArrayInputStream.available()]; // Create a new file that will contain the exported result. File file = new File(EXPORT_FILE); FileOutputStream fileOutputStream = new FileOutputStream(file); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( byteArrayInputStream.available()); int x = byteArrayInputStream.read(byteArray, 0, byteArrayInputStream.available()); byteArrayOutputStream.write(byteArray, 0, x); byteArrayOutputStream.writeTo(fileOutputStream); // Close streams. byteArrayInputStream.close(); byteArrayOutputStream.close(); fileOutputStream.close(); System.out .println("Successfully exported report to " + EXPORT_FILE); } catch (ReportSDKException ex) { ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } } 我甚至在导出调用之前添加了以下内容,但它仍然失败:

   oracle.jdbc.pool.OracleDataSource ds 
        = new oracle.jdbc.pool.OracleDataSource();
      ds.setDriverType("thin");
      ds.setServerName("server");
      ds.setPortNumber(123);
      ds.setDatabaseName("database");
      ds.setPassword("password");
      ds.setUser("user");

如前所述,当我使用“works_fine”报告时,它就像一个冠军,但是一旦我使用“有问题”的报告,就会发生这种情况: oracle.jdbc.pool.OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource(); ds.setDriverType("thin"); ds.setServerName("server"); ds.setPortNumber(123); ds.setDatabaseName("database"); ds.setPassword("password"); ds.setUser("user");

值得一提的是,我是Crystal Reports的新手并创建了JNDI名称。

非常感谢任何形式的帮助。感谢。

1 个答案:

答案 0 :(得分:0)

我通过覆盖连接属性解决了这个问题:

package com.org;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfoKind;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ITable;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.PropertyBag;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;

public class YetAnotherReader {

    public static void main(String[] args) {
        try {
            //Overwrite any existing properties with updated values.
            //information Oracle database
            String DBUSERNAME = "user";
            String DBPASSWORD = "password";
            String SERVERNAME = "server";
            String PORT = "1521";
            String DATABASE_NAME = "databaseName"; // SID or Instance
            String URI = "!oracle.jdbc.driver.OracleDriver!jdbc:oracle:thin:{userid}/{password}@" + SERVERNAME + ":" + PORT + "/" + DATABASE_NAME;  //1521/ or :1521
            String DATABASE_DLL = "crdb_jdbc.dll";
            //end

            String report_name = "C:\\myReport.rpt";
            String exportFileName = "C:\\fileName.pdf";
            ReportClientDocument clientDoc = new ReportClientDocument();
            clientDoc.open(report_name, ReportExportFormat._PDF);

            // Obtain collection of tables from this database controller.
            ITable table = clientDoc.getDatabaseController().getDatabase().getTables().getTable(0);

            IConnectionInfo connectionInfo = table.getConnectionInfo();
            PropertyBag propertyBag = connectionInfo.getAttributes();
            propertyBag.clear();

            //Overwrite any existing properties with updated values.
            propertyBag.put("Trusted_Connection", "false");
            propertyBag.put("Server Name", SERVERNAME); //Optional property.
            propertyBag.put("Database Name", DATABASE_NAME);
            propertyBag.put("Server Type", "JDBC (JNDI)");
            propertyBag.put("URI", URI);
            propertyBag.put("Use JDBC", "true");
            propertyBag.put("Database DLL", DATABASE_DLL);
            connectionInfo.setAttributes(propertyBag);

            //Set database username and password.
            //NOTE: Even if these the username and password properties don't change when switching databases, the 
            //database password is *not* saved in the report and must be set at runtime if the database is secured.  
            connectionInfo.setUserName(DBUSERNAME);
            connectionInfo.setPassword(DBPASSWORD);
            connectionInfo.setKind(ConnectionInfoKind.SQL);

            table.setConnectionInfo(connectionInfo);
            //Update old table in the report with the new table.
            clientDoc.getDatabaseController().setTableLocation(table, table);

            clientDoc.getDataDefController().getParameterFieldController().setCurrentValue("", "pk_id", 14);
            //Writing into PDF file
            ByteArrayInputStream bais = (ByteArrayInputStream) clientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
            int size = bais.available();
            byte[] barray = new byte[size];
            FileOutputStream fos = new FileOutputStream(new File(exportFileName));
            ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
            int bytes = bais.read(barray, 0, size);
            baos.write(barray, 0, bytes);
            baos.writeTo(fos);
            clientDoc.close();
            bais.close();
            baos.close();
            fos.close();
            //dbConn.close();

        } catch (ReportSDKException ex) {
            System.out.println("ReportSDKException" + ex);
        } catch (Exception ex) {
            System.out.println("Exception" + ex);
        }

    }

}

另外,请注意使用主键(您可能也想更改它):

clientDoc.getDataDefController().getParameterFieldController().setCurrentValue("", "pk_id", 14);

“pk_id”是报告中使用的参数。示例:

SELECT * FROM myTableOrView WHERE id < {?pk_id}

来源:Here