I’m using a JNDI for creating tomcat connection pool. It works great in a web application. I believe the InitialContext is provided by the tomcat server.
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup(“java:/comp/env”);
dataSource = (DataSource)envContext.lookup(“jdbc/testdb”);
But when I try to call the same utility from a standalone Java program, the initContext object is null. How can I explicitly provide all the necessary properties that Context object is expecting.
Error : javax.naming.NoInitialContextException: Need to specify class
name in environment or system property, or as an applet parameter, or
in an application resource file: java.naming.factory.initial
解决方案
You could also create your own custom context.
LocalContext ctx = LocalContextFactory.createLocalContext();
ctx.addDataSource(“jdbc/testdb”, driverName, url, usr, pwd);
UPDATE
Setup:
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind(“jdbc/Oracle”, ods);
builder.activate();
Use:
DataSource ds = InitialContext.doLookup(“jdbc/Oracle”);
今天的文章initialcontext java,独立Java程序中的Initialcontext分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/72771.html