Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to use JNDI datasource with subreport


    asimkin

    Question:

    How to use JNDI source as a subreport datasource.


    Answer:

    In order to use JNDI connection for subreport, you should create a custom Java code to retrieve the connection details and pass it to the subreport. The article below may be used as a base for the code

    http://www.javapractices.com/topic/TopicAction.do?Id=127 

    package com.custom;
    import java.sql.*;import javax.naming.*;import javax.sql.*;
    public class GetConnection {
      /** Uses JNDI and Datasource (preferred style).   */ 
    public static Connection getJNDIConnection(String dsContext){    
    //String DATASOURCE_CONTEXT = "jdbc/foodmart";        
    Connection result = null;      
    try {      
    Context initialContext = new InitialContext();      
    if ( initialContext == null){        
    log("JNDI problem. Cannot get InitialContext.");      
    }      
    DataSource datasource = (DataSource)initialContext.lookup(dsContext);      
    if (datasource != null) {        
    result = datasource.getConnection();      
    }      else {        
    log("Failed to lookup datasource.");      
    }
        }    
    catch ( NamingException ex ) {
          log("Cannot get connection: " + ex);
        }
        catch(SQLException ex){
          log("Cannot get connection: " + ex);
        }
        return result;
      }
      private static void log(Object aObject){
        System.out.println(aObject);
      }}
    

    import java.sql.*;import javax.naming.*;import javax.sql.*;

    public class GetConnection {

    /** Uses JNDI and Datasource (preferred style). */

    public static Connection getJNDIConnection(String dsContext){

    //String DATASOURCE_CONTEXT = "jdbc/foodmart";

    Connection result = null;

    try {

    Context initialContext = new InitialContext();

    if ( initialContext == null){

    log("JNDI problem. Cannot get InitialContext.");

    }

    DataSource datasource = (DataSource)initialContext.lookup(dsContext);

    if (datasource != null) {

    result = datasource.getConnection();

    } else {

    log("Failed to lookup datasource.");

    }

    }

    catch ( NamingException ex ) {

    log("Cannot get connection: " + ex);

    }

    catch(SQLException ex){

    log("Cannot get connection: " + ex);

    }

    return result;

    }

    private static void log(Object aObject){

    System.out.println(aObject);

    }}

    [/code]

    The next steps are:


    1. Edit source code per your requirements 

    2. Compile the java code to GetConnection.class, pack it to a jar file.

    3. Copy the jar archive to WEB-INFlib folder in JasperReports Server

    5. Restart JasperReports Server 

    6. In the main report specify subreport's connection expression like 


    com.custom.GetConnection.getJNDIConnection("java:comp/env/jdbc/foodmart") 


    Please note you should specify JNDI name as java:comp/env/jdbc/<NAME> 

    Also, the JNDI connection should be defined in application server.

    In case of JasperReports Server, it is defined in META-INFcontext.xml file. 


    Ref. Case #00052505

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...