首页 > 编程语言 >配置jndi服务,javax.naming.NamingException的四种情况

配置jndi服务,javax.naming.NamingException的四种情况

时间:2022-12-20 19:31:56浏览次数:39  
标签:java class jndi NamingException import naming javax

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;
  

 



标签:java,class,jndi,NamingException,import,naming,javax
From: https://blog.51cto.com/u_15147537/5956525

相关文章