title: Cannot connect to Oracle DB through Java Context method. \Error: Name [comp/env] is not bound in this Context. Unable to find [comp]
body: <p>I am learning Apache 9.0 web server and Oracle 11g DB within Eclipse EE environment.
  When I simply connect using</p>
  <pre><code>conn = DriverManager.getConnection(url, &quot;name&quot;, &quot;pw&quot;);
  </code></pre>
  <p>I can connect to Oracle DB and send SQL and receive result. (everything works.)</p>
  <p>But when I try to connect with this method below, I get error.</p>
  <pre><code>public class JdbcUtil {
  
  public static Connection getConnection() {
      Connection con = null;
  
      try {
          Context initCtx = new InitialContext();
          Context envCtx = (Context) initCtx.lookup(&quot;java:comp/env&quot;);
          DataSource ds = (DataSource) envCtx.lookup(&quot;jdbc/OracleDB&quot;);
          System.out.println(&quot;ds = &quot; + ds);
          
          con = ds.getConnection();
          System.out.println(&quot;con = &quot; + con);
          con.setAutoCommit(false);
          System.out.println(&quot;DB connect success! Util&quot;);
      } catch (Exception e) {
          System.out.println(&quot;DB connect failure! Util&quot;);
          e.printStackTrace();
      }
      return con;
  }
  </code></pre>
  <p>Error message is</p>
  <pre><code>javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
  at org.apache.naming.NamingContext.lookup(NamingContext.java:833)
  at org.apache.naming.NamingContext.lookup(NamingContext.java:174)
  at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
  at javax.naming.InitialContext.lookup(InitialContext.java:417)
  at db.JdbcUtil.getConnection(JdbcUtil.java:14)
  at service.mListService.memberList(mListService.java:15)
  at controller.mListController.doProcess(mListController.java:45)
  </code></pre>
  <p>So the error is at <code>Context envCtx = (Context) initCtx.lookup(&quot;java:comp/env&quot;);</code></p>
  <p>But sometimes error is at <code>DataSource ds = (DataSource) envCtx.lookup(&quot;jdbc/OracleDB&quot;);</code></p>
  <p>This is my context.xml in META-INF (of my current project)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;Context&gt;
  &lt;Resource
  name = &quot;jdbc/OracleDB&quot;
  auth = &quot;Container&quot;
  type = &quot;javax.sql.DataSource&quot;
  username = &quot;NAME&quot;
  password = &quot;PW&quot;
  driverClassName = &quot;oracle.jdbc.driver.OracleDriver&quot;
  factory = &quot;org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory&quot;
  url = &quot;jdbc:oracle:thin:@127.0.0.1:1521:xe&quot;
  maxActive =&quot;500&quot;
  maxIdle = &quot;100&quot;
  /&gt;
  &lt;/Context&gt;
  </code></pre>
  <p>This is my web.xml in WEB-INF/lib (of my current project)</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot; version=&quot;4.0&quot;&gt;
    &lt;display-name&gt;MemberBoard&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  &lt;resource-ref&gt;
   &lt;description&gt;Connection&lt;/description&gt;
   &lt;res-ref-name&gt;jdbc/OracleDB&lt;/res-ref-name&gt;
   &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
   &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  &lt;/web-app&gt;
  </code></pre>
  <p>I didn't touch xml files in my tomcat 9.0 server.</p>