Hi,
I have created a data source in web sphere console and now I am trying to get a database connection using this data source. I am getting exception while looking for the JNDI name of the data source.
Below are the code and exceptions
Code: -
DataSource ds = null;
Connection conn = null;
try{
Context context = new InitialContext();
//InitialContext context = new InitialContext();
ds = (DataSource)context.lookup("jdbc/XADataSource");
if (ds != null) {
conn = ds.getConnection();
System.out.println("Connected:");
}
}
Exception -
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an
applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:653)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:294)
at javax.naming.InitialContext.lookup(InitialContext.java:361)
at com.metlife.onlinepolicy.DBConnection.getConnection(DBConnection.java:30)
at com.metlife.onlinepolicy.DBConnection.main(DBConnection.java:47)
Code: -
Hashtable env = new Hashtable();
DataSource ds = null;
Connection conn = null;
try{
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL,"iiop://localhost:2809");
Context context = new InitialContext(env);
ds = (DataSource)context.lookup("jdbc/XADataSource");
if (ds != null) {
conn = ds.getConnection();
System.out.println("Connected:");
}
}
Exception:-
May 27, 2010 12:31:13 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 27, 2010 12:31:15 PM com.ibm.ws.naming.util.CommonHelpers
WARNING: jndiproviderProperties
May 27, 2010 12:31:15 PM com.ibm.websphere.naming.WsnInitialContextFactory
WARNING: noInitCtxFactoryImpl
javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.
at com.ibm.websphere.naming.WsnInitialContextFactory.init_implClassCtor(WsnInitialContextFactory.java:194)
at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(WsnInitialContextFactory.java:112)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:675)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:257)
at javax.naming.InitialContext.init(InitialContext.java:233)
at javax.naming.InitialContext.<init>(InitialContext.java:209)
at com.metlife.onlinepolicy.DBConnection.getConnection(DBConnection.java:24)
at com.metlife.onlinepolicy.DBConnection.main(DBConnection.java:47)
Please help.
Thanks,
J S Padhi