title: how to change Java code to access database using JDBC after creating JDBC connection pool in GlassFish?
body: <p>I'm new to JDBC. I've installed GlassFish 3.1.1 on Centos 6.2 and need to use it with an application that connects to an Oracle 11G database on another server. I've read through the documentation for GlassFish and think I understand how to create a JDBC connection pool as well as a JDBC resource. My question is, how do I use this information when coding the java middle-tier to connect to the database? </p>
  
  <p>Currently (with just GlassFish install and no JDBC configuration), I am relying on the CentOS enviroment variables for java (such as CLASSPATH) to allow the web application to use the JDBC drivers. However, I'm getting the following error:</p>
  
  <pre><code>java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource 
  </code></pre>
  
  <p>Thus, my attempt to create a JDBC connection pool and resource in GlassFish (so the app can use the JDBC driver). My java file starts out:</p>
  
  <pre><code>import java.sql.*;
  import oracle.jdbc.*;
  import oracle.jdbc.pool.OracleDataSource;
  
  class JDBCexample {
  
      public static void main(String args[]) throws SQLException {
              Connection conn;
              Statement stmt;
              ResultSet rset;
              String query;
              String sqlString;
  
              String person_firstName;
              String person_lastName;
              String person_email;
              int person_salary;
  
              // connect to database
              OracleDataSource ds = new OracleDataSource();
              ds.setURL("jdbc:oracle:thin:myID/myPWD@192.168.0.1:1521:mySID");
              conn = ds.getConnection();
  
              // read something in database
              stmt = conn.createStatement();
              query = "SELECT first_name, last_name, email, salary FROM HR.Employees where rownum &lt; 6";
              rset = stmt.executeQuery(query);
              while (rset.next()) {
                      person_firstName = rset.getString("first_name");
                      person_lastName = rset.getString("last_name");
                      person_email = rset.getString("email");
                      person_salary = rset.getInt("salary");
                      System.out.format(person_firstName + "  " + person_lastName + "  " + person_email + "  %d%n", person_salary) 
              }
  and so on...
  </code></pre>
  
  <p>QUESTION: How would I change the above code after I create a JDBC Connection Pool (named: myPool) and a JDBC Resource (named: myDBPool)? If it matters, I'm using Oracle 11.2, CentOS 6.2, GlassFish 3.1.1 with mod_jk and fronted by Apache 2.2 webserver, JDK 1.6. I don't have any clustering or load-balancing. </p>
  
  <p>UPDATE 1: I thought <a href="http://docs.oracle.com/cd/E11882_01/java.112/e16548/urls.htm" rel="nofollow">this link</a> was a good reference (see section titled: "Creating a Data Source Instance, Registering with JNDI, and Connecting"). But when I modify the above Java file as follows (just preparing the java file; haven't touched GlassFish yet),</p>
  
  <pre><code>// Add These:
  import javax.naming.Context;
  import javax.naming.InitialContext;
  
  // Change from this:
  // connect to database
      OracleDataSource ds = new OracleDataSource();
      ds.setURL("jdbc:oracle:thin:myID/myPWD@192.168.0.1:1521:mySID");
      conn = ds.getConnection();
  
  // To this:
  // connect to database
      Context ctext = new InitialContext();
      OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
      conn = ds.getConnection();
  </code></pre>
  
  <p>I get the errors:</p>
  
  <pre><code>JitterClass.java:67: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
                      Context ctext = new InitialContext();
                                      ^
  JitterClass.java:68: unreported exception javax.naming.NamingException; must be caught or declared to be thrown
                      OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
                                                                          ^
  </code></pre>
  
  <p>UPDATE 2: I cleared those compile errors using cyril's comments below (to throw all exceptions). Then I created JDBC Connection Pool and JDBC Resource, and the Ping was successful. So then I run the application from the client and observe the following error:</p>
  
  <pre><code>java.lang.ClassCastException : com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to oracle.jdbc.pool.OracleDataSource
  </code></pre>
  
  <p>At this point, if I add an <code>include javax.sql.DataSource</code> to the program, and change this line:</p>
  
  <pre><code>OracleDataSource ds = (OracleDataSource)ctext.lookup("jdbc/myDBPool");
  </code></pre>
  
  <p>to become this line:</p>
  
  <pre><code>DataSource ds = (DataSource)ctext.lookup("jdbc/myDBPool");
  </code></pre>
  
  <p>it compiles without errors. But now I'm confused... aren't we supposed to be using OracleDataSource here? Or, does GlassFish somehow implement OracleDataSource since I do see a setting for this connection pool for <code>Datasource Classname</code> set to <code>oracle.jdbc.pool.OracleDataSource</code> (?). Hoping someone can explain this.</p>