title: How do I connect to a Websphere Datasource with a given JNDI name?
body: <p>I'm using Websphere Portal 7.0 and creating a portlet with RAD 8.0. My portlet is trying to make a db2 connection to a remote server. I wrote a java program locally to do a basic JDBC connection to the server and get records from a table. The code works fine; however, when I add the code to my portlet as well as the db2jcc4.jar, the connection doesn't work. I'm using the basic: </p>
  
  <pre><code>Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
  </code></pre>
  
  <p>I figure that using the Websphere datasource is the right way to go. I know the JNDI name for the datasource, but I'm not finding clear cut examples on how to make a connection. Several examples use a DataSource class (I typed this in and this doesn't seem like it comes from a native java package so what import do I use here?) coupled with a Context. I've come across code like: </p>
  
  <pre><code>Context ctx = new InitialContext();
  ctx.lookup("jdbc/xxxx");
  </code></pre>
  
  <p>...
  Can someone break this down for me? </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I've updated my code per the answers listed. I really think I'm getting closer. Here is my getConnection() method:</p>
  
  <pre><code>private Connection getConnection() throws SQLException {
      javax.naming.InitialContext ctx = null;
      javax.sql.DataSource ds = null;
      System.out.println("Attempting connection..." + DateUtil.now() );
      try {
          ctx = new javax.naming.InitialContext();
          ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/db");
          connection = ds.getConnection();
      } catch (NamingException e) {
          System.out.println("peformanceappraisalstatus: COULDN'T CREATE CONNECTION!");
          e.printStackTrace();
      }       
      System.out.println("connection: " + connection.getClass().getName() + " at " + DateUtil.now());
      return connection;
  }
  </code></pre>
  
  <p>My entire web.xml file looks like: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
      &lt;display-name&gt;PeformanceAppraisalStatus&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;
          Datasource connection to Db&lt;/description&gt;
          &lt;res-ref-name&gt;jdbc/db&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;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
      &lt;/resource-ref&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I am seeing an error that describes the very thing you guys are telling me Websphere should prompt me to do, but doesn't:</p>
  
  <pre><code>SRVE0169I: Loading Web Module: PeformanceAppraisalStatus.
  [8/23/11 18:08:02:166 CDT] 00000009 InjectionProc E   CWNEN0044E: A resource reference binding could not be found for the jdbc/db resource reference, defined for the PeformanceAppraisalStatus component.
  [8/23/11 18:08:02:169 CDT] 00000009 InjectionEngi E   CWNEN0011E:  The injection engine failed to process bindings for the metadata.
  </code></pre>
  
  <p>Yes, I know that I've mispelled performance as peformance throughout the app.  </p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>I was so very close. Here are the missing bits that made it all fall into place:</p>
  
  <pre><code>web.xml:
  &lt;resource-ref&gt;      
      &lt;description&gt;
      Datasource connection to db&lt;/description&gt;
      &lt;res-ref-name&gt;jdbc/db&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;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
      &lt;mapped-name&gt;jdbc/db&lt;/mapped-name&gt;      
  &lt;/resource-ref&gt;
  
  ibm-web-bnd.xml:
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-bnd 
      xmlns="http://websphere.ibm.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
      version="1.0"&gt;
  
      &lt;virtual-host name="default_host" /&gt;
  
  
      &lt;resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" /&gt;
  &lt;/web-bnd&gt;
  </code></pre>
  
  <p>It appears that the ibm-web-bnd.xml file handles the binding between the project resource name and the datasource in websphere. Once I added the line:</p>
  
  <pre><code>&lt;resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" /&gt;
  </code></pre>
  
  <p>Websphere Portal seemed appeased. My code is working and connecting to the database now. </p>