java 连接 websphere_本地java类访问websphere的JNDI
# 问题1、获取数据源时报错
# javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet
# 代码:dataSource=(DataSource)envCtx.lookup("jdbc/admin");
# 原因:本地测试如果要访问远程的JNDI资源,就必须用饱含JNDI环境参数Hashtable初始化InitialContext。
# 必要的环境参数如:
# Context.INITIAL_CONTEXT_FACTORY//连接工厂
# Context.PROVIDER_URL//访问连接
# Context.SECURITY_PRINCIPAL//安全用户
# Context.SECURITY_CREDENTIALS//用户密码
问题2、配置InitialContext
# websphere 7
Hashtable env= new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "iiop://localhost:2809");
ctx = new InitialContext(env);
dataSource=(DataSource)context.lookup("jdbc/admin");
# websphere 5
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY,
com.ibm.websphere.naming.WsnInitialContextFactory.class.getName());
parms.put(Context.PROVIDER_URL, "iiop://localhost:900/");
Context ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("userdb");
//jboss:
Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"
Context.URL_PKG_PREFIXES, "org.jboss.naming"
Context.PROVIDER_URL, "localhost:1099"
//weblogic:
Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"
Context.PROVIDER_URL, "t3://localhost:7001"
//apusic(金蝶):
Context.INITIAL_CONTEXT_FACTORY, "com.apusic.jndi.InitialContextFactory"
Context.PROVIDER_URL, "rmi://localhost:6888"
//WebSphere 5:
Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:900"
//J2EE SDK(J2EE RI):
Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"
Context.PROVIDER_URL, "iiop://127.0.0.1:1050"
//SilverStream:
Context.INITIAL_CONTEXT_FACTORY, "com.sssw.rt.jndi.AgInitCtxFactory"
Context.PROVIDER_URL, "sssw://localhost:80"
//OC4J:
Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.rmi.RMIInitialContextFactory"
Context.PROVIDER_URL, "ormi://127.0.0.1/"
//WAS5:
Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:2809"
常用JNDI服务提供者连接工厂:
Filesystem: Com.sun.jndi.fscontext.FSContextFactory或者com.sun.jndi.fscontext.RefFSContextFactory
LDAPv3: Com.sun.jndi.ldap.LdapCtxFactory
NDS: com.novell.naming.service.nds.NdsInitialContextFactory
NIS: com.sun.jndi.nis.NISCtxFactory
RMI registry: com.sun.jndi.rmi.registry.RegistryContextFactory
IBM LDAP服务提供者: com.ibm.jndi.LDAPCtxFactory
BEA 名字服务提供者: weblogic.jndi.WLInitialContextFactory
JBOSS名字服务提供者: org.jnp.interfaces.NamingContextFactory
# 问题3、数据源配置成功后,Junit调试报错
# java.lang.NoClassDefFoundError:com/ibm/ws/bootstrp/raswsloggerFactory
# 原因:com.ibm.websphere.naming.WsnInitialContextFactory这个类找不到,需要把websphere的客户端使用的.jar包含在classpath中。
com.ibm.ws.ejb.thinclient_7.0.0.jar
# javax.naming.NamingException: Failed to initialize the ORB [Root exception is org.omg.CORBA.INITIALIZE: can't instantiate default ORB implementation com.ibm.CORBA.iiop.ORB vmcid: 0x0 minor code: 0 completed: No]
com.ibm.ws.orb_7.0.0.jar
#所需websphere环境中的jar包
com.ibm.ws.ejb.thinclient_7.0.0.jar
com.ibm.ws.orb_7.0.0.jar
(下面测试中未使用)
com.ibm.ws.runtime.jar
ibmorb.jar
总结:
我开始测试时只是在本地建了一个JAVA类并没有部署一个应用程序到websphere中。在websphere中部署一个应该程序后已经可以获得连接了。
不在websphere中部署应用程序而想测试连接是否成功,则需要在getConnection()方法中传入数据库用户名与密码。
如:
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY,
com.ibm.websphere.naming.WsnInitialContextFactory.class.getName());
parms.put(Context.PROVIDER_URL, "iiop://localhost:900/");
Context ic = new InitialContext();
ds = (DataSource) ic.lookup("jndi/dsc");
Connection conn = ds.getConnection("userdb","userdb");
com.ibm.ws.admin.client_7.0.0.jar
java.lang.NoClassDefFoundError: org/eclipse/wst/common/internal/emf/utilities/EncoderDecoder
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.ibm.ws.naming.util.WsnInitCtxFactory.decodeCredentials(WsnInitCtxFactory.java:778)
at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:428)
at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123)
at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52)
at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69)
at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.eclipse.wst.common.internal.emf.utilities.EncoderDecoder
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 45 more
com.ibm.ws.ejb.thinclient_7.0.0.jar
获取datasource失败
javax.naming.NamingException: Failed to initialize the ORB [Root exception is java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB]
at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:364)
at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:421)
at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123)
at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52)
at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69)
at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB
at com.ibm.ws.orb.GlobalORBFactory.init(GlobalORBFactory.java:92)
at com.ibm.ejs.oa.EJSORBImpl.initializeORB(EJSORBImpl.java:179)
at com.ibm.ejs.oa.EJSClientORBImpl.(EJSClientORBImpl.java:83)
at com.ibm.ejs.oa.EJSClientORBImpl.(EJSClientORBImpl.java:59)
at com.ibm.ejs.oa.EJSORB.init(EJSORB.java:102)
at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:356)
... 32 more
获取数据失败
java.lang.NullPointerException
at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69)
at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)
at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
java 连接 websphere_本地java类访问websphere的JNDI相关推荐
- java连接mongod抛java.net.SocketTimeoutException: connect timed out异常(2015-11-07 20:29:58)
为什么80%的码农都做不了架构师?>>> 背景:本机的eclipse项目访问操作安装在本机虚拟机ContOS6.5系统上的MongoDB 最近在用java连接mongod的时候 ...
- java连接mysql2008_在Java中如何使用jdbc连接Sql2008数据库(转)
我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...
- java中如何创建子包,父包类可以从java中的子包类访问?
In java parent package class accessible from child packge class? please explain me any one? example ...
- hive2 java连接_用Java代码通过JDBC连接Hiveserver2
1.在终端启动hiveserver2 #hiveserver2 2.使用beeline连接hive 另外打开一个终端,输入如下命令(xavierdb必须是已经存在的数据库) #beeline -u j ...
- java连接twitter登录,java – 通过Twitter登录OAuth不记得授权
我正在编写一个Web应用程序,并且已经实现了用户可以使用spring-social-(core / twitter)通过Twitter登录. 然而,Twitter表现得很奇怪.在初始身份验证/授权之后 ...
- Java连接Mysql(JDBC)
Java连接Mysql JDBC (Java DataBase Connection) 是通过JAVA访问数据库. java连接mysql数据库需要第三方的类, 大多数java的类包的后缀名都是jar ...
- Java 连接各种数据库
SQLite - Java 在 Java 程序中使用 SQLite 之前,我们需要确保机器上已经有 SQLite JDBC Driver 驱动程序和 Java. 从 sqlite-jdbc 库下载 s ...
- java连接MySQL几种方法_Java连接MySQL数据库三种方法
好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...
- Java连接MySQL
忙了一中午终于连接成功了O(∩_∩)O 菜啊,? 要连接首先需要打开数据库,"我用的是做php的时候下载的MySQL".2. 这里我用eclipse做的项目. Java连接MySQ ...
最新文章
- Docker - 在CentOS7.5中升级Docker版本
- 使用 C# 开发智能手机软件:推箱子(十二)
- matlab怎么设置x轴距,MATLAB及其在电气工程中的应用苏小林第四章.ppt
- 《深入浅出WPF》学习笔记之深入浅出话属性
- websocket的用途/场景
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 基于数据库资源的多语言实现...
- 没有它你的DevOps是玩不转的,你信不?
- html获取location,通过定义JS窗口对象获取url地址location.search部分的参数值
- Hamcrest 精萃
- 几款经典的免费小软件 -- 白领的左右手
- 从faker.js 开源作者Marak已删除了GIthub所有代码我们得到了什么教训
- win7系统计算机文件夹缓慢,windows7搜索文件非常慢的解决办法以及优化方法
- 必备:产品经理工作文档大全
- 房天下APP竞品分析
- 瞎折腾实录:构建 Armel 版本的 .NET Core 教程和资料资源
- javaweb JAVA JSP校园二手交易平台源码jsp二手交易系统 闲置物品出售
- ADI Blackfin DSP处理器-BF533的开发详解7:SPI接口的驱动和应用(含源代码)
- 如何最简洁的使用iOS 开发证书 和 Profile 文件
- eclipse打开文件的快捷键是什么
- HDU 4770 Lights Against Dudely