Introduction to JNDI (what is JNDI)

Source: Internet
Author: User
Introduction to JNDI (what is JNDI)
JNDI (Java Naming and Directory Interface) is an application Program The designed API provides developers with universal and unified interfaces for searching and accessing various naming and directory services. JDBC is built on the abstraction layer.
The available directories and services that can be accessed by JNDI are:
DNS, xnam, Novell Directory Service, LDAP (Lightweight Directory Access Protocol light Directory Access Protocol), the Registry, RMI, DSML V1 & V2, and NIS of the CORBA object service, file system, Windows XP/2000/NT/ME/9x.
JNDI advantages:
It contains a large number of naming and directory services and uses common interfaces to access different types of services;
You can connect to multiple naming or directory services at the same time;
Establish a logical association. The name can be associated with a Java object or resource without guiding the physical ID of the object or resource.
JNDI package: javax. Naming: Name operation;
Javax. Naming. Directory: Directory operation;
Javax. Naming. Event: Request Event Notification in the named directory server;
Javax. Naming. LDAP: Provides LDAP support;
Javax. Naming. spi: allows dynamic insertion of different implementations.

The JNDI naming and service functions are used to provide enterprise-level APIs access to naming and services. For example, ejbs, JMS, JDBC 2.0, and RMI on IIOP use the name service of CORBA through JNDI.
JNDI and JDBC:
JNDI provides a unified method for searching and accessing services on the network. By specifying a resource name, the name corresponds to a record in the database or naming service, and returns the information required for database connection establishment.
CodeExample: Try {
Context cntxt = new initialcontext ();
Datasource DS = (datasource) cntxt. Lookup ("JDBC/DPT ");
}
Catch (namingexception ne ){
...
}

JNDI and JMS:
Message Communication is a method used by software components or applications to communicate. JMS is a Java technology that allows applications to create, send, receive, and read messages.
Sample Code: Try {
Properties Env = new properties ();
Initialcontext inictxt = new initialcontext (ENV );
Topicconnectionfactory connfactory = (topicconnectionfactory) inictxt. Lookup ("ttopicconnectionfactory ");
...
}
Catch (namingexception ne ){
...
}

Access a specific directory: for example, a person is an object and has several attributes, such as the name, phone number, email address, and zip code of the person. Getattributes () method

Attribute ATTR =
Directory. getattributes (personname). Get ("email ");
String email = (string) ATTR. Get ();

By using JNDI, let the customer use the object name or attribute to find the object: foxes = directory. Search ("O = Wiz, c = us", "Sn = Fox", controls );

Use JNDI to search for objects such as printers and databases and the printer example: printer = (printer) namespace. Lookup (printername );
Printer. Print (document );

Browse namespace: namingenumeration list = namespace. List ("O = widget, c = us ");
While (list. hasmore ()){
Nameclasspair entry = (nameclasspair) List. Next ();
Display (entry. getname (), entry. getclassname ());
}

Common JNDI operations: void BIND (string sname, object);-binding: The process of associating names with objects
Void rebind (string sname, object);-rebind: Used to rebind an object with an existing name
Void unbind (string sname);-release: Used to release an object from the directory.
Void Lookup (string sname, object);-query: returns an object of the total directory.
Void Rename (string soldname, string snewname);-Rename: used to modify the name bound to the object name
Namingenumeration listbinding (string sname);-list: list of objects bound to a specific context
Namingenumeration list (string sname );

Code example: Get the name, class name, and bound object again. Namingenumeration namenumlist = ctxt. listbinding ("cntxtname ");
...
While (namenumlist. hasmore ()){
Binding BND = (binding) namenumlist. Next ();
String sobjname = BND. getname ();
String sclassname = BND. getclassname ();
Someobject objlocal = (someobject) BND. GetObject ();
}

 

JNDI Connection database model

Package dbutil;

Import java. SQL. connection;
Import java. SQL. statement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import javax. SQL. datasource;
Import javax. Naming. context;
Import javax. Naming. initialcontext;

Public class dbconnection {
Private connection conn = NULL;
Private statement stmt = NULL;
Private resultset rs = NULL;
Private int resultnum = 0;

/**
* Constructor
* Locate the data source and create a connection with the data source.
*/
Public dbconnection (){
Try {
Context CTX = new initialcontext ();
If (CTX = NULL) throw new exception ("no context ");
Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/oracle ");
If (DS = NULL) throw new exception ("JDBC/Oracle is an unknown datasource ");
Conn = Ds. getconnection ();
Stmt = conn. createstatement ();
} Catch (exception e ){
System. Out. println ("Naming:" + E. getmessage ());
}
}

/**
* Execute an SQL statement: query records
* @ Param SQL statement
* @ Return resultset record set
*/
Public resultset executequery (string SQL ){
Rs = NULL;
Try {
Rs = stmt.exe cutequery (SQL );
} Catch (sqlexception SE ){
System. Out. println ("query error:" + Se. getmessage ());
}
Return Rs;
}

/**
* Execute SQL statements: insert and update records
* @ Param SQL statement
* @ Return int number of records updated by resultnum
*/
Public int executeupdate (string SQL ){
Resultnum = 0;
Try {
Resultnum = stmt.exe cuteupdate (SQL );
} Catch (sqlexception SE ){
System. Err. println ("Update error:" + Se. getmessage ());
}
Return resultnum;
}

/**
* Close the connection.
*/
Public void close (){
Try {
If (RS! = NULL ){
Rs. Close ();
Rs = NULL;
}
If (stmt! = NULL ){
Stmt. Close ();
Stmt = NULL;
}
If (Conn! = NULL ){
Conn. Close ();
Conn = NULL;
}
} Catch (sqlexception SE ){
System. Out. println ("Close error:" + Se. getmessage ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.