Java: JDBC & JNDI: Retrieving a Context

Posted on 2005-05-16
Last Modified: 2013-11-24

Am trying to retrieve a DataSource object (once it has been bound).

I believe I manage to bind the object successfully - a .bindings file is created in the relevant directory.
However, when I try to retrieve the object (doing a lookup), nothing is returned.

Maybe I'm just oversimplifying things...the MyDataSource class just extends DataSource and doesn't do much else.

Relevant code below:

      MyDataSource ds = new MyDataSource()
      ds.setDescription("Data Source");
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
      env.put(Context.PROVIDER_URL, "file:c:\\JDBCDataSource");
      Context ctx = new InitialContext(env);

      // Register the data source to JNDI naming service
      // for application to use
      ctx.ind("jdbc/Oracle", ds);

    // The following call will return null
     DataSource  new_ds = (DataSource) ctx.lookup(jdbc/Oracle);


Question by:context
    1 Comment
    LVL 2

    Accepted Solution

    try this solution

    Context ic = new InitialContext();
           java.lang.Object objref =


    private String dbName = "java:comp/env/jdbc/Oracle";

    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup(dbName);

    Connection con =  ds.getConnection();


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now