Jump to content
We've recently updated our Privacy Statement, available here ×
  • Jaspersoft Report using two separate data sources.


    Tirupathi Mangalarapu
    • Features: Custom Data Sources, Data Sources, Reports Version: v8, v8.0, v8.0.0, v8.0.1 Product: JasperReports® Server

    Normally $P{REPORT_CONNECTION} parameter is used to pass the main report's database to the subreport for the same report connection. A subreport can use a different JDBC connection than the main report. 

    There are several ways to accomplish this requirement:

    Why this approach: One of our customers did not want to expose connecting string or username password information in the .jrxml file nor they do not want to use the data adapter approach.

    In this approach, we need to create two parameters to create a new database connection. I will detail the steps below.

    For this wiki, I am using two different JDBC data sources for my report one is a sample database that comes with TIBCO Jaspersoft sample database “SugarCRM” it’s a PostgreSQL database and the other is an MSSQL database.

    Here are the steps I followed:

    Create a report in Jaspersoft Studio using “Sugarcrm” with the following query:

    SELECT id,

                    date_entered,

                    name,

                    modified_user_id,

                    date_modified

    FROM accounts

    Read fields, preview the report, and run the report.

    Create two parameters in this report:

    1. $P{DATA_SOURCE_PATH}

    Class: com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource

    Default value expression:

    com.jaspersoft.jasperserver.api.engine.jasperreports.util.RepositoryUtil.getThreadRepositoryContext().getRepository().getResource(null,"/public/Samples/Data_Sources/DB_MSSQL")

    Here “/public/Samples/Data_Sources/DB_MSSQL” is the path to the JDBC data source that is created on the Jaspersoft

    Server. (Right click go to properties and copy the path of the JDBC data source from Jaspersoft server)

    Attached a screenshot for your reference below:

    converted-image.png.4c28bb0ca67d7f91a4b3185a31e31730.png

    Screenshot of the param below:

    converted-image.png.ba94ce5d4f4819ff27131953f034a0ca.png

     

     

    1. $P{SUBREPORT_CONNECTION}

    class: java.sql.Connection

    default value expression: “java.sql.DriverManager.getConnection($P{DATA_SOURCE_PATH}.getConnectionUrl(),$P{DATA_SOURCE_PATH}.getUsername(),$P{DATA_SOURCE_PATH}.getPassword())”

    converted-image.png.bcde396296b165158e0806232c62eea8.png

     

    Either you can create a sub report separately or you can add use the drag and drop subreport element and start from a blank sub-report (it’s up to you).

    Just to keep it easier, I will create sub report separately with MSSQL data source and bring in some fields and add them to the detail band. Preview it and ensure that the report runs and brings in some data as shown below:

    converted-image.png.9bf58a762f092b7375a88b942b0d2e5d.png

    converted-image.png.49785c4682ad63d6b58d75403e16f229.png

    Once the sub report is created, I will change my Data Adapter to not use any data source as shown below:

    converted-image.png.fe3cedd88266152b6f412265e249038e.png

     

    The purpose of the above steps is to ensure that the metadata of the fields are read and the report has fields added to the report from your second data source i.e. MSSQL in this case.

    Now, go ahead and drag and drop sub report component from the palette into the summary section of the main report. Select an existing report and point to the report that you have created using the MSSQL in the above step (using workspace resource).

    click “subreport” section

    You should see something like this:

    converted-image.png.3b1db49ae4c1044519f006c72bee9d43.png

     

     

    Now go ahead and change the value of the connection expression. Change it to $P{SUBREPORT_CONNECTION}

    We are passing the connection string from the main report to the sub report using the parameters.

    Now, preview the main report in the Jaspersoft Studio. You may see errors like this:

    converted-image.png.a92fb72ed24d342224ccf6d5eedb66a1.png

    I did not research more on the error messages. If anyone has any input let us know in the comment section(We might need to add all the jars from the Jaspersoft server to be added to the classpath on this project in the studio)

    Go ahead and publish this report to the Jaspersoft Server, and test it on the server.

    You should see your report running without any errors. Here is the screenshot of the report from my instance.converted-image.png.4d85b8ecc71b62d7c93967d08ab3017d.png

    converted-image.png.281b61c50c9844f02161bb681baad5ea.png

     

     

    Note: I attached the jrxml files to this KBA so that you can take this as a reference if you run into any challenges during configuration on your end.  This is tested in the latest version of the JRS 8.0

    Hope this helps someone!

     

    diffdbtype-postgresql-mssql.jrxml

    diffdbtype-subreport.jrxml


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...