error with JNDI & Tomcat 5

Discussions

General J2EE: error with JNDI & Tomcat 5

  1. error with JNDI & Tomcat 5 (1 messages)

    i am new to DataSource and Connection Pool,
    i am trying to get a connection pool from the datasource i follwoed the instructions posted on jakarta web site:
    http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
     but i am getting error:
    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

    i am sure my java code:
    InitialContext ctx = new InitialContext();
                  
                  if(ctx == null )
                  System.out.println(" no context");
                  System.out.println(" good context");
                  DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
                  
                  if (ds != null) {
                     System.out.println(ds);
                    conn = ds.getConnection();
                  }
            } catch (NamingException ex) {
                  ex.printStackTrace();
            }catch (SQLException e) {
                   e.getMessage();
                   e.printStackTrace();
            }
    my web.xml :
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/TestDB</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
      

    but i am not sure if i have to add an <environment> tag to the server.xml (See the link above) if i do plz show me an example .
    if any body has experience with this problem plz help me.
  2. http://forum.java.sun.com/thread.jspa?threadID=467376&messageID=2151261


    here's my setup:

    <Server>
    ...
      <GlobalNamingResources>
    ...
        <Resource name="jdbc/foo" type="javax.sql.DataSource"/>
    ...
        <ResourceParams name="jdbc/foo">
          <parameter>
            <name>url</name>
            <value>jdbc:mysql://localhost/foo</value>
          </parameter>
          <parameter>
            <name>username</name>
            <value>user</value>
          </parameter>
          <parameter>
            <name>password</name>
            <value>pass</value>
          </parameter>
          <parameter>
            <name>driverClassName</name>
            <value>com.mysql.jdbc.Driver</value>
          </parameter>
          <parameter>
    ... [other db connection info]

    datasource jndi name (I use hibernate, so can't help you with the context):
    java:comp/env/jdbc/foo