Websphere Portal 7.0 を使用しており、RAD 8.0 でポートレットを作成しています。ポートレットがリモート サーバーへの db2 接続を確立しようとしています。サーバーへの基本的な JDBC 接続を行い、テーブルからレコードを取得する Java プログラムをローカルで作成しました。コードは正常に動作します。しかし、ポートレットと db2jcc4.jar にコードを追加すると、接続が機能しません。私は基本を使用しています:
Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
Websphere データソースを使用するのが正しい方法だと思います。データソースの JNDI 名は知っていますが、接続方法に関する明確な例が見つかりません。いくつかの例では、DataSource クラスを使用しています (これを入力しましたが、これはネイティブ Java パッケージからのものではないようです。ここではどのインポートを使用しますか?)。次のようなコードに出くわしました:
Context ctx = new InitialContext();
ctx.lookup("jdbc/xxxx");
... 誰かが私のためにこれを分解できますか?
編集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;
}
私の 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>
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.
はい、アプリ全体でパフォーマンスのつづりをパフォーマンスと間違えていることは承知しています。
解決
私はとても親密でした。すべてが適切な位置に収まるようになった欠落しているビットは次のとおりです。
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>
ibm-web-bnd.xml ファイルは、プロジェクトのリソース名と websphere のデータソースの間のバインディングを処理しているようです。行を追加したら:
<resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
Websphere Portal は落ち着いたようです。私のコードは機能しており、現在データベースに接続しています。