Home Tutorial Java Jdbc Registering DataSource with JNDI

 
 

Share on Google+Share on Google+
Registering DataSource with JNDI
Posted on: October 20, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn how register a JDBC DataSource with JNDI

Registering The DataSource With JNDI

Java Naming Directory Interface (JNDI)

Java Naming Directory Interface (JNDI) is an API of java technology which provides naming and directory functionality to java applications. It is designed using Java Object Model for java applications. With the name of JNDI java application can store and retrieve named java object of any type. It provides methods for performing standard directory operation, for example searching for objects for their associating attributes etc.

An Example of DataSource registration with JNDI is given below

JNDIWithDataSourceExample.java

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.ConnectionPoolDataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class RegisteringJNDIWithDataSource {
	private static void startRegistry() throws RemoteException {
		System.out.println(LocateRegistry.getRegistry());
		LocateRegistry.createRegistry(1059);
		System.out.println("RMI registry Stared.");
	}

	private static InitialContext createInitialContextContext()
			throws NamingException {
		Properties properties = new Properties();
		properties.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.rmi.registry.RegistryContextFactory");
		properties.put(Context.PROVIDER_URL, "rmi://localhost:1059");
		InitialContext initialContextcontext = new InitialContext(properties);
		return initialContextcontext;
	}

	public static void main(String args[]) {
		try {
			startRegistry();
			ConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
			((MysqlDataSource) dataSource).setUser("root");
			((MysqlDataSource) dataSource).setPassword("root");
			((MysqlDataSource) dataSource).setServerName("192.168.10.13");
			((MysqlDataSource) dataSource).setPort(3306);
			((MysqlDataSource) dataSource).setDatabaseName("student");

			InitialContext context = createInitialContextContext();
			context.rebind("Source", dataSource);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
When you run this application it will display message as shown below:

RegistryImpl_Stub[UnicastRef [liveRef: [endpoint:[192.168.10.97:1099](remote),objID:[0:0:0, 0]]]]
RMI registry Stared.

Download this example code

Advertisement

Related Tags for Registering DataSource with JNDI:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com