title: JDBC Connection Pooling Clarification
body: <p>I have set up jdbc connection pooling in a java-ee environment by doing the following changes.</p>
  
  <pre><code>    The context.xml 
  
  &lt;Context&gt; 
          &lt;Resource name="jdbc/mysybase" auth="Container" 
                    type="javax.sql.DataSource" driverClassName="com.sybase.jdbc3.jdbc.SybDriver" 
                    url="jdbc:sybase:Tds:H2S33.studtrack.com:2025/student" 
                    username="scott" password="tiger" maxActive="20" maxIdle="10" 
                    maxWait="-1"/&gt; 
      &lt;/Context&gt; 
  
  
      In The web.xml file 
      &lt;resource-ref&gt; 
       &lt;description&gt;Sybase Datasource example&lt;/description&gt; 
       &lt;res-ref-name&gt;jdbc/mysybase&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; 
  
  
      And the jsp page 
  
      &lt;%@page import="java.sql.*"%&gt; 
      &lt;%@page import="javax.naming.Context"%&gt; 
      &lt;%@page import="javax.naming.InitialContext"%&gt; 
      &lt;%@page import="java.sql.Connection"%&gt; 
      &lt;%@page import="java.sql.SQLException"%&gt; 
      &lt;%@page import="java.sql.ResultSet"%&gt; 
      &lt;%@page import="javax.sql.DataSource"%&gt; 
      &lt;html&gt; 
      &lt;head&gt; 
      &lt;title&gt;Obtaining a Connection&lt;/title&gt; 
      &lt;/head&gt; 
      &lt;body&gt; 
  
      &lt;% 
          Connection conn = null; 
          ResultSet result = null; 
          Statement stmt = null; 
           try { 
             Context initContext = new InitialContext(); 
          Context envContext  = (Context)initContext.lookup("java:/comp/env"); 
             DataSource ds = (DataSource)envContext.lookup("jdbc/mysybase"); 
             conn = ds.getConnection(); 
          if (conn != null)  
          { 
              String message = "Got Connection " + conn.toString() + ", "; 
              out.write(message); 
          } 
          else 
          { 
              out.write("hello no conn obtained"); 
  
          } 
  
              stmt = conn.createStatement(); 
              result = stmt.executeQuery("SELECT * FROM Student"); 
          while(result.next()) 
          { 
              out.write(result.getString("name")); 
          } 
  
           } 
           catch (SQLException e) { 
               out.write("Error occurred " + e); 
            } 
  
      %&gt; 
  
      &lt;/body&gt; 
      &lt;/html&gt;
  </code></pre>
  
  <p>Now i want the jdbc pooling to be available in normal java classes as well.
  Do i need to make any changes if i want the pooling to be available in java classes.
  Can i get a connection object in a java class just as i got the connection in the jsp as shown above.</p>
  
  <pre><code>Connection conn = null; 
  ResultSet result = null; 
  Statement stmt = null;
  Context initContext = new InitialContext(); 
  Context envContext  = (Context)initContext.lookup("java:/comp/env"); 
  DataSource ds = (DataSource)envContext.lookup("jdbc/mysybase"); 
  conn = ds.getConnection(); 
  </code></pre>