title: jdbc javax.naming.NameNotFoundException
body: <p>I have been having a problem with this exception for a long time.  I am trying to connect to an external mysql database but I keep getting this exception.  I have looked at many examples and I still have not found the answer.  Here is my code, please bare in mind that this is all the code I use for this so please say if I've missed something out:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;context&gt;
      &lt;!-- Registers Database with JNDI Naming Service as a pooled DataSource --&gt;
  &lt;Resource 
          name="jdbc/DBNAME" 
          auth="Container" 
          type="javax.sql.DataSource" 
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          validationInterval="30000"
          timeBetweenEvictionRunsMillis="30000"
          maxActive="100" 
          minIdle="10" 
          maxWait="10000" 
          initialSize="10"
          removeAbandonedTimeout="60"
          removeAbandoned="true"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000" 
          jmxEnabled="true"
          jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
          username="****" 
          password="********"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://****:3306/****"/&gt;
  
  &lt;/context&gt;
  </code></pre>
  
  <p>And here is my code for calling it:</p>
  
  <pre><code>try
  {
      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DBNAME");
      Connection c = ds.getConnection();
      return c;
  }
  catch(NamingException ne)
  {
      System.out.println(ne.toString());
      return null;
  }
  catch (SQLException se) 
  {
      System.out.println(se.toString());
      return null;
  }
  </code></pre>
  
  <p>Usernames, passwords, db urls and connection names have been redacted but the rest is correct.</p>