110 likes | 211 Views
J ava N aming and D irectory I nterface. Matt. What is JNDI. JNDI. JNDI 提供了命名 (naming) 與目錄 (directory) 服務,它的功用在於可以對整個環境中的資源進行命名,並透過命名取得該資源。
E N D
JNDI • JNDI提供了命名(naming)與目錄(directory)服務,它的功用在於可以對整個環境中的資源進行命名,並透過命名取得該資源。 • The SPI provides a plug−in architecture with which third parties can use to develop support for additional naming and directory services and integrate that support seamlessly into the JNDI framework. The SPI defines the methods they use to hook into the JNDI API. As a developer, all you need to do in order to use the service provider is load the library during runtime or at startup. • JNDI is used to find resources (such as EJBs) you have registered via the J2EE server. Advanced use of JNDI supports sophisticated storage and retrieval of Java objects and other information. • A Naming Service provides a mechanism for giving names to objects so that you can retrieve and use those objects without knowing the location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation. • A Directory Service also associates names with objects but provides additional information by associating attributes with the objects.
JNDI • One of the key tasks in a distributed application is finding items such as components, resources, message queues, and databases. • The Java Naming and Directory Interface (JNDI) is an essential part of J2EE applications, providing a mechanism for finding distributed components—but JNDI is good for more than just looking up EJB interfaces. It also allows access to important enterprise systems such as directory servers.
JNDI API Package • javax.naming • Defines classes and interfaces that enable you to interact with naming services. • javax.naming.directory • Defines classes and interfaces that enable you to interact with directory services. • javax.naming.event • Defines classes and interfaces that handle event notifications when you’re working with naming and directory services. • javax.naming.ldap • Defines classes and interfaces for working with LDAP v3.0. • javax.naming.spi • Defines the service−provider interface that vendors of naming and directory service providers must implement.
The first step in using the JNDI name service is to get a context in which to add or find names. • The context that represents the entire namespace is called the Initial Contextand is represented by a class called javax.naming.InitialContext and is a sub-class of the javax.naming.Context class. Context ctx = new InitialContext(); • A Context object represents a context that you can use to look up objects or add new objects to the namespace.
JNDI example import javax.naming.*; import javax.sql.*; import java.sql.*; //use JNDI(Java Naming and Directory Interface) public class Lookup { public Lookup() { Connection con = null; Statement stmt = null; ResultSet rs = null; try { InitialContext ic = new InitialContext(); Context envCtx = (Context)ic.lookup("java:comp/env"); //java:computer environment DataSource ds = (DataSource)envCtx.lookup("jdbc/music"); //查找jdbc/music con = ds.getConnection(); //得到connection stmt = conn.createStatement(); //得到statement rs = stmt.executeQuery("select * from music"); while (rs.next()) { //do something } rs.close(); stmt.close(); con.close(); } catch (Exception ex) { System.err.println("ERROR"); } } }