wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Websphere
Bookmark "JNDI Lookup help in websphere 4.0" Watch "JNDI Lookup help in websphere 4.0" New topic
Author

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.
Thanks
Neha
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","ibm.com.??. CNWASInitialcontextFactory");
prop.put("ProviderURL","iiop:\\IP:900");
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: 1850
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, "com.ibm.websphere.naming.WsnInitialContextFactory");
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");}

Thanks
Neha

Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
ds = (javax.sql.DataSource)ctx.lookup("jdbc/OracleDSN");
try the following
ds = (javax.sql.DataSource)ctx.lookup("OracleDSN");
Rahul
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.*;
//import com.ibm.websphere.appserver.cm.factory.*;
public class AccessDataSource{
public static void main(String args[]){
try{
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
p.put(Context.PROVIDER_URL,"iiop://192.9.226.243:900");
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");
//System.out.println(ds.getDatabaseName());
//System.out.println(ds.getDataSourceName());
//System.out.println(ds.getURL());
//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");
System.out.println("Executed");
while(rset.next()){
BigDecimal bd = rset.getBigDecimal("tsid");
System.out.println(bd);
}
stmt.close();
con.close();
}
catch(NamingException ne){
ne.printStackTrace();
}
catch(SQLException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
}
[This message has been edited by Jim Yingst (edited September 13, 2001).]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
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...
KyleBrown
------------------
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 http://www.kyle-brown.com/ for other WebSphere information.
 
 
subject: JNDI Lookup help in websphere 4.0