Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RAD6 datasource access using JNDI

 
Harikrishna kishore Harkala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Iam writing code like given below for datasource access using JNDI

public class DataSourceJNDI {

public static void main(String[] args)throws SQLException,NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("jdbc/CMPDS");
Connection con=ds.getConnection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT NAME,ID,DEPT FROM EMPLOY");
String name=rs.getString("NAME");
String id=rs.getString("ID");
String dept=rs.getString("DEPT");
System.out.println(name);
System.out.println(dept);
System.out.println(id);

}
}
I am getting aruntime exception like this:
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory. Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:375)
at java.lang.ClassLoader.loadClass(ClassLoader.java:562)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:442)
at java.lang.ClassLoader.loadClass(ClassLoader.java:494)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:60)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
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 Ray.DataSourceJNDI.main(DataSourceJNDI.java:25)

Please any one can help me?
 
sudheer babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Harikrishna kishore Harkala:
When Iam writing code like given below for datasource access using JNDI

public class DataSourceJNDI {

public static void main(String[] args)throws SQLException,NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("jdbc/CMPDS");
Connection con=ds.getConnection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT NAME,ID,DEPT FROM EMPLOY");
String name=rs.getString("NAME");
String id=rs.getString("ID");
String dept=rs.getString("DEPT");
System.out.println(name);
System.out.println(dept);
System.out.println(id);

}
}
I am getting aruntime exception like this:
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory. Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:375)
at java.lang.ClassLoader.loadClass(ClassLoader.java:562)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:442)
at java.lang.ClassLoader.loadClass(ClassLoader.java:494)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:60)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
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 Ray.DataSourceJNDI.main(DataSourceJNDI.java:25)

Please any one can help me?
 
sudheer babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
did you check whether the class name you have given for the intial context is the same as you have provided. also check whether that class file is provided in the class directory.
if all these are correct, then better check for the web.xml file for the context variable.
hope you will find the solution in one of these...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic