title: Place datasource password outside context.xml
body: <p>Here is my context.xml -</p>
  
  <pre><code>&lt;Context docBase=“myDB” path="/myDB" reloadable="true"&gt;
        &lt;Resource name="jdbc/OracleDS"
            auth="Container"
            type="javax.sql.DataSource"
            factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
            driverClassName="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@test.asia-pacific.com:1546:myTest”
            accessToUnderlyingConnectionAllowed="true"
            maxActive="10" 
            maxIdle="5" 
            maxwait="-1"
            removeAbandoned="true" 
            removeAbandonedTimeout="60"
            logAbandoned="true"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>I don't want to add my DB username, and password to this file for security reasons. I have the below Java code -</p>
  
  <pre><code>Connection getConnection() {
  
      Connection con = null;
      DataSource ds = null;
      try {
          if (getJNDIName() != null) {
  
              Hashtable env = new Hashtable();                
              env.put(Context.SECURITY_PRINCIPAL, "username");
              env.put(javax.naming.Context.SECURITY_CREDENTIALS, "password");
              Context initCtx = new InitialContext(env);
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
  
              ds = (DataSource) envCtx.lookup(getJNDIName());
              if (ds != null) {
                  con = ds.getConnection();
              }
              if (ds == null || con == null) {
                  throw new RuntimeException(
                          "DataSource and/or Connection object is null.");
              }
  
              return con;
          } else if (cd_.con_ != null) {
              return cd_.con_;
          } else {
              return null;
          }
      } catch (Exception e) {
          System.out.println(e.printStackTrace());
      }
  
      return null;
  }
  </code></pre>
  
  <p>When I start my application, I am seeing below errors in the log file -</p>
  
  <pre><code>Caused by: java.sql.SQLException: ORA-01017: invalid username/password; logon denied
  
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:389)
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:382)
  at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:600)
  at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:445)
  at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
  at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
  at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:380)
  at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:760)
  at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
  </code></pre>
  
  <p>If I put the username and password back in context.xml, it works like a charm. How do I put it outside the file. I would only want to put it in my Java class as the password will be encrypted, and not in xml since decrypting password from xml is not simple, and I don't want to get into it. Can someone please suggest how do I achieve this? Please</p>