1.当jndi服务没有启动,或者jndi服务的属性没有设置正确,抛出如下异常: javax.naming.CommunicationException: Can‘t find SerialContextProvider...
2.如果InitialContext class既没有为jndi服务提供者指出默认属性也没有明确指明属性,抛出如下异常: javax.naming.NoInitialContextException:Need to specify class name in enenvironment or system property,or as an appletparameter, or in an application resource file: java.naming java.naming.factory.initial
3.如果jndi程序的classpath没有包括jndi server class,抛出如下异常: javax.naming.NoInitialContextException: Cannot instantiate class: XXX [Root exception is java.lang.ClassNotFoundException: XXX]
4.jndi属性与jndi服务提供者不匹配,抛出如下异常: javax.naming.ServiceUnavailableException: Connection refused: no further information [Root exception is java.net.ConnectionException: Connection refused:no further information]
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
Context jndiContext = new InitialContext();
String jndiName = "java:/" + user.toLowerCase();
ds = (DataSource)jndiContext.lookup(jndiName);
con = ds.getConnection();
Connection localConnection = con;
return localConnection;