javax.naming.Context.lookup()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(152)

本文整理了Java中javax.naming.Context.lookup()方法的一些代码示例,展示了Context.lookup()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.lookup()方法的具体详情如下:
包路径:javax.naming.Context
类名称:Context
方法名:lookup

Context.lookup介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

DataSource dataSource = null;
try
{
  Context context = new InitialContext();
  dataSource = (DataSource) context.lookup("Database");
}
catch (NamingException e)
{
  // Couldn't find the data source: give up
}

代码示例来源:origin: apache/geode

public static void destroyTable(String tableName) throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
 Connection conn = ds.getConnection();
 // System.out.println (" trying to drop table: " + tableName);
 String sql = "drop table " + tableName;
 Statement sm = conn.createStatement();
 sm.execute(sql);
 conn.close();
}

代码示例来源:origin: quartz-scheduler/quartz

ctx = (props != null) ? new InitialContext(props): new InitialContext(); 
  ds = ctx.lookup(url);
  if (!isAlwaysLookup()) {
    this.datasource = ds;
  return (((XADataSource) ds).getXAConnection().getConnection());
} else if (ds instanceof DataSource) { 
  return ((DataSource) ds).getConnection();
} else {
  throw new SQLException("Object at JNDI URL '" + url + "' is not a DataSource.");

代码示例来源:origin: stackoverflow.com

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");

代码示例来源:origin: quartz-scheduler/quartz

ctx = (props != null) ? new InitialContext(props): new InitialContext(); 
  ds = ctx.lookup(url);
  if (!isAlwaysLookup()) {
    this.datasource = ds;
  return (((XADataSource) ds).getXAConnection().getConnection());
} else if (ds instanceof DataSource) { 
  return ((DataSource) ds).getConnection();
} else {
  throw new SQLException("Object at JNDI URL '" + url + "' is not a DataSource.");

代码示例来源:origin: apache/geode

public static void listTableData(String tableName) throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
 String sql = "select * from " + tableName;
 Connection conn = ds.getConnection();
 Statement sm = conn.createStatement();
 ResultSet rs = sm.executeQuery(sql);
 while (rs.next()) {
  System.out.println("id " + rs.getString(1) + " name " + rs.getString(2));
 }
 rs.close();
 conn.close();
}

代码示例来源:origin: stackoverflow.com

@Bean
public DataSource dataSource() throws Exception {
  Context ctx = new InitialContext();
  return (DataSource) ctx.lookup("java:jboss/datasources/mySQLDB");
}

代码示例来源:origin: stackoverflow.com

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");

// Allocate and use a connection from the pool
Connection conn = ds.getConnection();

代码示例来源:origin: apache/geode

/**
 * This method is used to return number rows from the timestamped table created by createTable()
 * in CacheUtils class.
 */
public int getRows(String tableName) throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
 String sql = "select * from " + tableName;
 Connection conn = ds.getConnection();
 Statement sm = conn.createStatement();
 ResultSet rs = sm.executeQuery(sql);
 int counter = 0;
 while (rs.next()) {
  counter++;
  // System.out.println("id "+rs.getString(1)+ " name "+rs.getString(2));
 }
 rs.close();
 conn.close();
 return counter;
}

代码示例来源:origin: stackoverflow.com

@Bean(destroyMethod = "")
public DataSource dataSource() throws NamingException{
  Context context = new InitialContext();
  return (DataSource)context.lookup("jdbc.mydatasource");
}

代码示例来源:origin: davidmoten/rxjava-jdbc

@Override
public Connection get() {
  try {
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup(jndiResource);
    Connection conn = ds.getConnection();
    return conn;
  } catch (SQLException e) {
    throw new SQLRuntimeException(e);
  } catch (NamingException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/geode

public static void createTable(String tableName) throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
 // String sql = "create table " + tableName + " (id number primary key, name varchar2(50))";
 // String sql = "create table " + tableName + " (id integer primary key, name varchar(50))";
 String sql = "create table " + tableName
   + " (id integer NOT NULL, name varchar(50), CONSTRAINT the_key PRIMARY KEY(id))";
 System.out.println(sql);
 Connection conn = ds.getConnection();
 Statement sm = conn.createStatement();
 sm.execute(sql);
 sm.close();
 sm = conn.createStatement();
 for (int i = 1; i <= 100; i++) {
  sql = "insert into " + tableName + " values (" + i + ",'name" + i + "')";
  sm.addBatch(sql);
  System.out.println(sql);
 }
 sm.executeBatch();
 conn.close();
}

代码示例来源:origin: stackoverflow.com

Context initialContext = new InitialContext();
String myvar = (String) initialContext.lookup("java:comp/env/myvar");

代码示例来源:origin: larsga/Duke

/**
 * Get a configured database connection via JNDI.
 */
public static Statement open(String jndiPath) {
 try {
  Context ctx = new InitialContext();
  DataSource ds = (DataSource) ctx.lookup(jndiPath);
  Connection conn = ds.getConnection();
  return conn.createStatement();
 } catch (NamingException e) {
  throw new DukeException("No database configuration found via JNDI at " +
              jndiPath, e);
 } catch (SQLException e) {
  throw new DukeException("Error connecting to database via " +
              jndiPath, e);
 }
}

代码示例来源:origin: apache/geode

/**
 * This method is used to delete all rows from the timestamped table created by createTable() in
 * CacheUtils class.
 */
public int deleteRows(String tableName) throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource da = (DataSource) ctx.lookup("java:/SimpleDataSource"); // doesn't req txn
 Connection conn = da.getConnection();
 Statement stmt = conn.createStatement();
 int rowsDeleted = 0; // assume that rows are always inserted in CacheUtils
 String sql = "";
 sql = "select * from " + tableName;
 ResultSet rs = stmt.executeQuery(sql);
 if (rs.next()) {
  sql = "delete from  " + tableName;
  rowsDeleted = stmt.executeUpdate(sql);
 }
 rs.close();
 stmt.close();
 conn.close();
 return rowsDeleted;
}

代码示例来源:origin: stackoverflow.com

import javax.naming.Context;
import javax.naming.InitialContext;
...
// Get the base naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");

// Get a single value
String dbhost = (String)env.lookup("dbhost");

代码示例来源:origin: javalite/activejdbc

/**
 * Opens a connection from JNDI based on a registered name. This assumes that there is a <code>jndi.properties</code>
 * file with proper JNDI configuration in it.
 *
 * @param jndiName name of a configured data source.
 */
public DB open(String jndiName) {
  checkExistingConnection(name);
  try {
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup(jndiName);
    Connection connection = ds.getConnection();
    LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
    ConnectionsAccess.attach(name, connection, jndiName);
    return this;
  } catch (Exception e) {
    throw new InitException("Failed to connect to JNDI name: " + jndiName, e);
  }
}

代码示例来源:origin: apache/geode

/**
 * This method is used to search for pattern which is the PK in the timestamped table created by
 * createTable() in CacheUtils class.
 */
public boolean checkTableAgainstData(String tableName, String pattern)
  throws NamingException, SQLException {
 Context ctx = cache.getJNDIContext();
 DataSource ds = (DataSource) ctx.lookup("java:/SimpleDataSource");
 boolean found = false;
 String id_str = "";
 String sql = "select * from " + tableName;
 Connection conn = ds.getConnection();
 Statement sm = conn.createStatement();
 ResultSet rs = sm.executeQuery(sql);
 while (rs.next()) {
  System.out.println("id:" + rs.getString(1));
  System.out.println("name:" + rs.getString(2));
  id_str = rs.getString(1);
  if (id_str.equals(pattern)) {
   found = true;
   break;
  } else
   continue;
 }
 rs.close();
 conn.close();
 return found;
}

代码示例来源:origin: stackoverflow.com

Hashtable env = new Hashtable(5);
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
//Assuming weblogic server is running on localhost at port 7001
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ic = new InitialContext(env);
//obtain a reference to the home or local home interface
FooHome fooHome = (FooHome)ic.lookup("MyBeans/FooHome");
//Get a reference to an object that implements the beans remote (component) interface
Foo foo = fooHome.create();
//call the service exposed by the bean
foo.shoutFoo()

代码示例来源:origin: javalite/activejdbc

/**
 * Opens a new connection from JNDI data source by name using explicit JNDI properties. This method can be used in cases
 * when file <code>jndi.properties</code> cannot be easily updated.
 *
 * @param jndiName name of JNDI data source.
 * @param jndiProperties JNDI properties
 */
public DB open(String jndiName, Properties jndiProperties) {
  checkExistingConnection(name);
  try {
    Context ctx = new InitialContext(jndiProperties);
    DataSource ds = (DataSource) ctx.lookup(jndiName);
    Connection connection = ds.getConnection();
    LogFilter.log(LOGGER, LogLevel.DEBUG, "Opened connection: " + connection);
    ConnectionsAccess.attach(name, connection,
        jndiProperties.contains("url") ? jndiProperties.getProperty("url") : jndiName);
    return this;
  } catch (Exception e) {
    throw new InitException("Failed to connect to JNDI name: " + jndiName, e);
  }
}

相关文章