Context.INITIAL_CONTEXT_FACTORY : Context « javax.naming « Java by API






Context.INITIAL_CONTEXT_FACTORY

   
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;

public class PooledConnectionExample {

  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
      connection = getConnection();
      statement = connection.createStatement();
      String selectEmployeesSQL = "SELECT * FROM employees";
      resultSet = statement.executeQuery(selectEmployeesSQL);

      while (resultSet.next()) {
        printEmployee(resultSet);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      closeAll(resultSet, statement, connection);
    }
  }
//Obtain connection from pool
  private static Connection getConnection() throws NamingException, SQLException {
    InitialContext initCtx = createContext();
    String jndiName = "HrDS";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    return pooledConnection.getConnection(); 
  }

  private static InitialContext createContext() throws NamingException {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
    env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
    InitialContext context = new InitialContext(env);
    return context;
  }

  private static void printEmployee(ResultSet resultSet) throws SQLException {
    System.out.print(resultSet.getInt("employee_id"));
    System.out.print(", ");
    System.out.print(resultSet.getString("last_name"));
    System.out.print(", ");
    System.out.print(resultSet.getString("first_name"));
    System.out.print(", ");
    System.out.println(resultSet.getString("email"));
  }

  private static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
    if (resultSet != null) {
      try {
        resultSet.close();
      } catch (SQLException e) {
      } // nothing we can do
    }
    if (statement != null) {
      try {
        statement.close();
      } catch (SQLException e) {
      } // nothing we can do
    }
    if (connection != null) {
      try {
        connection.close();
      } catch (SQLException e) {
      } // nothing we can do
    }
  }
}

           
         
    
    
  








Related examples in the same category

1.Context.PROVIDER_URL
2.Context.SECURITY_AUTHENTICATION
3.Context.SECURITY_CREDENTIALS
4.Context.SECURITY_PRINCIPAL
5.Context: addToEnvironment(String propName, Object propVal)
6.Context: close()
7.Context: createSubcontext(String name)
8.Context: destroySubcontext(String name)
9.Context: getNameInNamespace()
10.Context: getNameParser(String name)
11.Context: listBindings(String name)
12.Context: lookup(String name)
13.Context: rename(String oldName, String newName)
14.Context: unbind(String name)