javax.naming.NoInitialContextException using tomcat server


Web tier: servlets, JSP, Web frameworks: javax.naming.NoInitialContextException using tomcat server

  1. hi, i am working in jndi datasource for establishing database connection. but i am not able to connect database, bcoz i got the below error. how can i clear the error? i am using tomcat 5.5 server with eclipse editor. here is my coding: `````````````````` import; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class testJDBC { public static void main(String[] args) { Connection con=null; DataSource ds=null; Context initContext=null, envContext=null; try { initContext = new InitialContext(); }catch(Exception e) { System.out.println("InitContext Exception"); e.printStackTrace(); } try { envContext = (Context)initContext.lookup("java:comp/env"); }catch(Exception e) { System.out.println("envContext Exception"); e.printStackTrace(); } try { ds = (DataSource)envContext.lookup("jdbc/mySQLServer"); }catch(Exception e) { System.out.println("DataSource Cast envContext Exception"); e.printStackTrace(); } try { con=ds.getConnection(); }catch(Exception e) { System.out.println("get conntection from datasource Exception"); e.printStackTrace(); } try { if (con!=null) { System.out.println("Connection Successfull"); }else { System.out.println("Connection Not Successfull"); } }catch(Exception e) { System.out.println("conntection checking Exception"); e.printStackTrace(); } } } here is the error details : ```````````````````````````` 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(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.lookup(Unknown Source) at testJDBC.main( java.lang.NullPointerException at testJDBC.main( java.lang.NullPointerException at testJDBC.main( plz help me thanks and regards, k.s.kumar

    Threaded Messages (3)

  2. I look at the code but it seems like you are creating a standalone application (void main()) and not a class that is part of a Tomcat Web application. When you do that it may not be able to access the JNDI environment and resources of Tomcat. Perhaps I am wrong, but maybe you can try to create the method as a class file that is called from a servlet (context listener perhaps to initialize) or jsp file. Hope this helps
  3. Help me too[ Go to top ]

    the problem u faced here me too had it.. i'm using tomcat as server but the application is not working..
    Kindly tell wht u did to resolve it..
  4. continued[ Go to top ]

    its tomcat 5.5 server using netbeans.