jas*_*son 21 java db2 websphere jndi ibm-rational
我正在使用Websphere Portal 7.0并使用RAD 8.0创建一个portlet.我的portlet正在尝试与远程服务器建立db2连接.我在本地编写了一个java程序来与服务器建立基本的JDBC连接,并从表中获取记录.代码工作正常; 但是,当我将代码添加到我的portlet以及db2jcc4.jar时,连接不起作用.我正在使用基本的:
Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
Run Code Online (Sandbox Code Playgroud)
我认为使用Websphere数据源是正确的方法.我知道数据源的JNDI名称,但我没有找到关于如何建立连接的明确示例.有几个例子使用了一个DataSource类(我输入了这个类,这看起来好像它来自一个原生的java包,所以我在这里使用什么导入?)加上一个Context.我遇到过如下代码:
Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");
Run Code Online (Sandbox Code Playgroud)
...有人可以为我打破这个吗?
编辑1
我根据列出的答案更新了我的代码.我真的觉得我越来越近了.这是我的getConnection()方法:
private Connection getConnection() throws SQLException {
javax.naming.InitialContext ctx = null;
javax.sql.DataSource ds = null;
System.out.println("Attempting connection..." + DateUtil.now() );
try {
ctx = new javax.naming.InitialContext();
ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/db");
connection = ds.getConnection();
} catch (NamingException e) {
System.out.println("peformanceappraisalstatus: COULDN'T CREATE CONNECTION!");
e.printStackTrace();
}
System.out.println("connection: " + connection.getClass().getName() + " at " + DateUtil.now());
return connection;
}
Run Code Online (Sandbox Code Playgroud)
我的整个web.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>PeformanceAppraisalStatus</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>
Datasource connection to Db</description>
<res-ref-name>jdbc/db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我看到一个错误,描述了你们告诉我Websphere应该提示我做的事情,但不是:
SRVE0169I: Loading Web Module: PeformanceAppraisalStatus.
[8/23/11 18:08:02:166 CDT] 00000009 InjectionProc E CWNEN0044E: A resource reference binding could not be found for the jdbc/db resource reference, defined for the PeformanceAppraisalStatus component.
[8/23/11 18:08:02:169 CDT] 00000009 InjectionEngi E CWNEN0011E: The injection engine failed to process bindings for the metadata.
Run Code Online (Sandbox Code Playgroud)
是的,我知道我在整个应用程序中将性能误导为性能.
解
我非常亲密.以下是缺少的部分,使它全部落到实处:
web.xml:
<resource-ref>
<description>
Datasource connection to db</description>
<res-ref-name>jdbc/db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<mapped-name>jdbc/db</mapped-name>
</resource-ref>
ibm-web-bnd.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
version="1.0">
<virtual-host name="default_host" />
<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
</web-bnd>
Run Code Online (Sandbox Code Playgroud)
似乎ibm-web-bnd.xml文件处理项目资源名称与websphere中的数据源之间的绑定.一旦我添加了该行:
<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
Run Code Online (Sandbox Code Playgroud)
Websphere Portal似乎很平庸.我的代码现在正在工作并连接到数据库.
she*_*ley 14
您需要在应用程序中定义资源引用,然后在部署期间将该逻辑资源引用映射到物理资源(数据源).
在您的web.xml
帐户中添加以下配置(根据需要修改名称和属性):
<resource-ref>
<description>Resource reference to my database</description>
<res-ref-name>jdbc/MyDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)
然后,在应用程序部署期间,WAS将提示您将此资源引用(jdbc/MyDB
)映射到您在WAS中创建的数据源.
在您的代码中,您可以获得类似于您在示例中显示它的方式的DataSource; 但是,您将用于查找它的JNDI名称实际上应该是您定义的资源引用名称(res-ref-name
),而不是物理数据源的JNDI名称.此外,您还需要在res-ref-name前加上应用程序命名上下文(java:comp/env/
).
Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
110927 次 |
最近记录: |