📄 bind.java
字号:
import javax.naming.*;
import java.io.File;
import java.util.Hashtable;
/**
* Demonstrates how to add a binding to a context.
* (Use Rebind example to overwrite binding; use Unbind to remove binding.)
*
* usage: java Bind
*/
class Bind {
public static void main(String[] args) {
// Set up the environment for creating the initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:/tmp/tutorial");
try {
// Create the initial context
Context ctx = new InitialContext(env);
// Create the object to be bound
Car car = new Car("taxi");
// Perform the bind
ctx.bind("favorite", car);
// Check that it is bound
Object obj = ctx.lookup("favorite");
System.out.println(obj);
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.out.println("Operation failed: " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -