配置jndi服务,javax.naming.NamingException的四种情况
1.當(dāng)jndi服務(wù)沒(méi)有啟動(dòng),或者jndi服務(wù)的屬性沒(méi)有設(shè)置正確,拋出如下異常: javax.naming.CommunicationException: Can't find SerialContextProvider...
2.如果InitialContext class既沒(méi)有為jndi服務(wù)提供者指出默認(rèn)屬性也沒(méi)有明確指明屬性,拋出如下異常: 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沒(méi)有包括jndi server class,拋出如下異常: javax.naming.NoInitialContextException: Cannot instantiate class: XXX [Root exception is java.lang.ClassNotFoundException: XXX]
4.jndi屬性與jndi服務(wù)提供者不匹配,拋出如下異常: 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;
??
?
總結(jié)
以上是生活随笔為你收集整理的配置jndi服务,javax.naming.NamingException的四种情况的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一个简单好看的备忘录
- 下一篇: IntelliJ IDEA 超级全实用技