my dog learned polymorphism
The moose likes Websphere and the fly likes JNDI Lookup help in websphere 4.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark "JNDI Lookup help in websphere 4.0" Watch "JNDI Lookup help in websphere 4.0" New topic

JNDI Lookup help in websphere 4.0

Neha Sharma
Ranch Hand

Joined: Jul 13, 2001
Posts: 126
Hi Guys,
I want to use JNDI lookup to use a datasource created in Websphere 4.0 and I'm getting a null exception. All I'm doing in my dataconnection bean is

try {
InitialContext ic = new InitialContext();
datasource = (DataSource) ic.lookup("OracleDSN");
Con = datasource.getConnection();
} catch
Can anybody help me what should I write to access this datasource "OracleDSN". I don't have any other thing in my code, do I need to define the LDAp hash table etc.
Jun Hong
Ranch Hand

Joined: Sep 05, 2001
Posts: 181
InitialContext need to be configured by properties. First, you need to specify the initial context factory. Second, you need provider URL. Third, user name. Forth, password.
I cannot remember the exact codes. It is some thing like.
Properties prop = new Properties();
prop.put("InitialContextFactory"," CNWASInitialcontextFactory");
This is useless.
Tomorrow, I will post the exact code for you. I have it in my office.
Jun Hong

Jun Hong<br />SCJP, SCJD, SCWCD, SCEA<br />IBM Certified Systems Expert(V4.0)
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1868
Neha ,
I think that your data source object has not been created. You have not initialized it properly and it does not work. Try writing a servlet that accesses the Data source before writing the EJB.
Here is the sample code of my servlet

Please make the appropriate changes like my lookup is "oracle1" and the sql query you are executing
InitialContext initialcontext = new InitialContext();
The above line is ok
Neha Sharma
Ranch Hand

Joined: Jul 13, 2001
Posts: 126
Thanks Rahul and Jun,
Now I'm trying the following, and I'm still getting a null pointer Exception. I've just created the DSN but in the websphere documentation it says you have to bind your dsn to the JNDI how do you do that ? do you perform this programmatically or is there a way to do it in websphere.
java.util.Properties parms = new java.util.Properties();
parms.setProperty Context.INITIAL_CONTEXT_FACTORY, "");
Context ctx = new javax.naming.InitialContext(parms);
ds = (javax.sql.DataSource)ctx.lookup("jdbc/OracleDSN");
Con = ds.getConnection();
}catch (NullPointerException ex) {out.println("NullPointerException Exception while creating the datasource");}
catch (Exception ex) {out.println("Exception while creating the datasource");}


Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1868
ds = (javax.sql.DataSource)ctx.lookup("jdbc/OracleDSN");
try the following
ds = (javax.sql.DataSource)ctx.lookup("OracleDSN");
Jun Hong
Ranch Hand

Joined: Sep 05, 2001
Posts: 181
Sorry, I am out of my office because of attacks on WTC. I think the null point comes from ds. You did not find datasouce, it is null. When you codes call ds.getConnection(), null pointer exception is thrown. What did you use to prepare datasoure? I think you used an admin console to prepare it, right? If you did it using console, you specify a JNDI name there. jdbc is default name space there. Use that name don't change it. Even your datasoure and your Java client are on the same machine, your need to map your initial context to you local machine. Try props.put("ProviderURL", "iiop://localhost:900"); You should get ObjectNotFound Exception at least.
Jun Hong
Ranch Hand

Joined: Sep 05, 2001
Posts: 181
Finally, I am in my office.
Here is a testing program I wrote for WAS 3.5.
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import oracle.jdbc.pool.*;
import java.math.*;
public class AccessDataSource{
public static void main(String args[]){
Properties p = new Properties();
Context ctx = new InitialContext(p);
//OracleDataSource ds = (OracleDataSource)ctx.lookup("jdbc/Fusion");
//DataSource ds2 = (DataSource)ctx.lookup("jdbc/FusionSybaseDataSource");
//System.out.println("Find Sybase DataSource");
//Connection con1 = ds2.getConnection("dba","vertigo");
//System.out.println("get Sybase connection");
DataSource ds = (DataSource)ctx.lookup("jdbc/App DataSource");
System.out.println("Find Oracle DataSource");
//Connection con = ds.getConnection("user","passwd");
Connection con = ds.getConnection("user2","passwd2");
System.out.println("Get Oracle Connection");
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery("select distinct tsid from fsdEquities where tsid=1713");
BigDecimal bd = rset.getBigDecimal("tsid");
catch(NamingException ne){
catch(SQLException se){
catch(Exception e){
[This message has been edited by Jim Yingst (edited September 13, 2001).]
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
Are you certain that you have successfully created the DataSource? For instance, it's common to forget to save the configuration file in AEs (and thus the change doesn't take effect). If you are in WebSphere 4.0 AEs, shutdown the Application server, restart it, then go browse the JDBC Driver and look to see if the datasource is visible...
Kyle Brown,
Editor of Enterprise Java (tm) Programming with IBM Websphere

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
I agree. Here's the link:
subject: JNDI Lookup help in websphere 4.0
It's not a secret anymore!