Java by API/javax.naming/InitialContext — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:43, 31 мая 2010

InitialContext: lookup(String name)

 
/* 
 * */
import java.sql.Connection;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
  public static void main(String[] args) throws Exception {
    String dsn = args[0];
    System.out.println("Attempting to connect to " + dsn);
    try {
      System.out.println("Initializing the naming context...");
      InitialContext init = new InitialContext();
      System.out.println("Looking up " + dsn);
      DataSource source = (DataSource) init.lookup(dsn);
      System.out.println("Establishing a connection...");
      Connection connection = source.getConnection();
      System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





new InitialContext()

 
/* 
 * */
import java.sql.Connection;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
  public static void main(String[] args) throws Exception {
    String dsn = args[0];
    System.out.println("Attempting to connect to " + dsn);
    try {
      System.out.println("Initializing the naming context...");
      InitialContext init = new InitialContext();
      System.out.println("Looking up " + dsn);
      DataSource source = (DataSource) init.lookup(dsn);
      System.out.println("Establishing a connection...");
      Connection connection = source.getConnection();
      System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}





new InitialContext(Hashtable<?, ?> environment)

 
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 Main {
  public static void main(String[] args) throws Exception {
    Connection connection = getConnection();
    // Do work with connection
    Statement statement = connection.createStatement();
    String selectEmployeesSQL = "SELECT * FROM employees";
    ResultSet resultSet = statement.executeQuery(selectEmployeesSQL);
    while (resultSet.next()) {
      printEmployee(resultSet);
    }
    resultSet.close();
    statement.close();
    connection.close();
  }
  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(); // Obtain connection from pool
  }
  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(resultSet.getString("last_name") + ", ");
    System.out.print(resultSet.getString("first_name") + ", ");
    System.out.println(resultSet.getString("email"));
  }
}