title: How to make JDBC connectivity
body: <p>I am using the follwing class as my connection manager to  make DB connectvity through datasource. The problem is that when I invoke this class through my DAOImpl class it is returning null. </p>
  
  <pre><code>private static DataSource dataSource;
  private static Connection connection;
  
  private ConnectionFactory() {
      System.out.println(" ConnPoolFactory cons is called ");
  }
  
  public static synchronized Connection getConnection() throws SQLException {
  
      try {
  
          if (connection == null) {
              Context ctx = new InitialContext();
              Context envContext = (Context) ctx.lookup("java:/comp/env");
  
  
              dataSource = (DataSource) envContext.lookup("jdbc/myoracle");
              connection = dataSource.getConnection();
          } else {
              return connection;
          }
  
      } catch (NamingException e) {
          e.printStackTrace();
      }
      System.out.println(connection);
      return connection;
      //System.out.println(connection);
  
  } 
  </code></pre>
  
  <ul>
  <li>I am able to make this class work if I use <code>DriverManager</code> for conectivity.</li>
  <li>I am able to achieve JDBC connectvity through servlet class using a  <code>DataSource</code>.</li>
  </ul>
  
  <p>But with the above code I get the following exception:</p>
  
  <blockquote>
    <p>javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  </p>
  </blockquote>
  
  <p>the following is my context.xml</p>
  
  <pre>
  &lt;Resource auth="Container" 
            driverClassName="oracle.jdbc.OracleDriver" 
            name="jdbc/myoracle" 
            password="password" 
            type="javax.sql.DataSource" 
            url="jdbc:oracle:thin:@10.49.116.42:1521:DBNAME" 
            username="username"/&gt;
  </pre>