Using Resource Adapters from an Application
Your application also can use resource adapters directly instead of using them through a container-managed, application server environment. The following code example shows how your application can access an Oracle database directly using a resource adapter:
import java.util.Hashtable; import java.sql.Connection; import javax.sql.DataSource; import javax.naming.*; import com.ddtek.resource.jdbc.JCAConnectionFactory; import com.ddtek.resource.jdbc.spi.*; public class RAExample { static public void main(String[] args) { try { // Create a connection factory instance OracleManagedConnectionFactory managedFactory = new OracleManagedConnectionFactory(); managedFactory.setServerName("MyOracleServer"); managedFactory.setPortNumber("1521"); managedFactory.setSID("TEST"); JCAConnectionFactory factory = (JCAConnectionFactory) managedFactory.createConnectionFactory(); // Get an InitialContext. // Using File System JNDI Service Provider as an example Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:c:/ConnectionFactories"); Context connectorContext = new InitialContext(env); // Bind the connection factory try { connectorContext.bind("OracleConnectionFactory", factory); except) { connectorContext.rebind("OracleConnectionFactory", factory); } } catch (Exception except) { System.out.println("Error creating DataSource"); System.exit(0); } // Connect via the DataSource try { // Get an InitialContext. // Using File System JNDI Service Provider as an example Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL, "file:c:/ConnectionFactories"); Context connectorContext = new InitialContext(env); // Lookup the connection factory DataSource dataSource = (DataSource) connectorContext.lookup("OracleConnectionFactory"); Connection connection = dataSource.getConnection("scott", "tiger"); } catch (Exception except) { System.out.println("Looking up connection factory"); } } }