<tfoot id='e7sgA'></tfoot>
  • <i id='e7sgA'><tr id='e7sgA'><dt id='e7sgA'><q id='e7sgA'><span id='e7sgA'><b id='e7sgA'><form id='e7sgA'><ins id='e7sgA'></ins><ul id='e7sgA'></ul><sub id='e7sgA'></sub></form><legend id='e7sgA'></legend><bdo id='e7sgA'><pre id='e7sgA'><center id='e7sgA'></center></pre></bdo></b><th id='e7sgA'></th></span></q></dt></tr></i><div id='e7sgA'><tfoot id='e7sgA'></tfoot><dl id='e7sgA'><fieldset id='e7sgA'></fieldset></dl></div>

        • <bdo id='e7sgA'></bdo><ul id='e7sgA'></ul>
      1. <small id='e7sgA'></small><noframes id='e7sgA'>

      2. <legend id='e7sgA'><style id='e7sgA'><dir id='e7sgA'><q id='e7sgA'></q></dir></style></legend>

        如何使用给定的 JNDI 名称连接到 Websphere 数据源?

        How do I connect to a Websphere Datasource with a given JNDI name?(如何使用给定的 JNDI 名称连接到 Websphere 数据源?)
          <tbody id='I9slp'></tbody>

            <tfoot id='I9slp'></tfoot>

            <small id='I9slp'></small><noframes id='I9slp'>

          • <legend id='I9slp'><style id='I9slp'><dir id='I9slp'><q id='I9slp'></q></dir></style></legend>
          • <i id='I9slp'><tr id='I9slp'><dt id='I9slp'><q id='I9slp'><span id='I9slp'><b id='I9slp'><form id='I9slp'><ins id='I9slp'></ins><ul id='I9slp'></ul><sub id='I9slp'></sub></form><legend id='I9slp'></legend><bdo id='I9slp'><pre id='I9slp'><center id='I9slp'></center></pre></bdo></b><th id='I9slp'></th></span></q></dt></tr></i><div id='I9slp'><tfoot id='I9slp'></tfoot><dl id='I9slp'><fieldset id='I9slp'></fieldset></dl></div>

                  <bdo id='I9slp'></bdo><ul id='I9slp'></ul>
                • 本文介绍了如何使用给定的 JNDI 名称连接到 Websphere 数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Websphere Portal 7.0 并使用 RAD 8.0 创建一个 portlet.我的 portlet 正在尝试与远程服务器建立 db2 连接.我在本地编写了一个 java 程序来与服务器建立基本的 JDBC 连接并从表中获取记录.代码工作正常;但是,当我将代码添加到我的 portlet 以及 db2jcc4.jar 时,连接不起作用.我正在使用基本的:

                  I'm using Websphere Portal 7.0 and creating a portlet with RAD 8.0. My portlet is trying to make a db2 connection to a remote server. I wrote a java program locally to do a basic JDBC connection to the server and get records from a table. The code works fine; however, when I add the code to my portlet as well as the db2jcc4.jar, the connection doesn't work. I'm using the basic:

                  Connection connection = DriverManager.getConnection("jdbc:db2://server:port/db:user=user;password=pw;");
                  

                  我认为使用 Websphere 数据源是正确的方法.我知道数据源的 JNDI 名称,但我没有找到关于如何建立连接的明确示例.几个例子使用了一个 DataSource 类(我输入了这个,这似乎不是来自本地 java 包,所以我在这里使用什么导入?)加上一个 Context.我遇到过这样的代码:

                  I figure that using the Websphere datasource is the right way to go. I know the JNDI name for the datasource, but I'm not finding clear cut examples on how to make a connection. Several examples use a DataSource class (I typed this in and this doesn't seem like it comes from a native java package so what import do I use here?) coupled with a Context. I've come across code like:

                  Context ctx = new InitialContext();
                  ctx.lookup("jdbc/xxxx");
                  

                  ...有人可以帮我分解一下吗?

                  ... Can someone break this down for me?

                  编辑 1

                  我已根据列出的答案更新了我的代码.我真的觉得我越来越近了.这是我的 getConnection() 方法:

                  I've updated my code per the answers listed. I really think I'm getting closer. Here is my getConnection() method:

                  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 文件看起来像:

                  My entire web.xml file looks like:

                  <?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 应该提示我做的事情,但没有:

                  I am seeing an error that describes the very thing you guys are telling me Websphere should prompt me to do, but doesn't:

                  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.
                  

                  是的,我知道我在整个应用程序中将性能拼写为性能.

                  Yes, I know that I've mispelled performance as peformance throughout the app.

                  解决方案

                  我是如此接近.以下是使其全部到位的缺失部分:

                  I was so very close. Here are the missing bits that made it all fall into place:

                  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 中的数据源之间的绑定.一旦我添加了这一行:

                  It appears that the ibm-web-bnd.xml file handles the binding between the project resource name and the datasource in websphere. Once I added the line:

                  <resource-ref name="jdbc/db" binding-name="jdbc/mydatasource" />
                  

                  Websphere Portal 似乎得到了安抚.我的代码现在正在工作并连接到数据库.

                  Websphere Portal seemed appeased. My code is working and connecting to the database now.

                  推荐答案

                  您需要在您的应用程序中定义一个资源引用,然后在执行期间将该逻辑资源引用映射到物理资源(数据源)部署.

                  You need to define a resource reference in your application and then map that logical resource reference to the physical resource (data source) during deployment.

                  在您的 web.xml 中,添加以下配置(根据需要修改名称和属性):

                  In your web.xml, add the following configuration (modifying the names and properties as appropriate):

                  <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>
                  

                  然后,在应用程序部署期间,WAS 会提示您将此资源引用(jdbc/MyDB)映射到您在 WAS 中创建的数据源.

                  Then, during application deployment, WAS will prompt you to map this resource reference (jdbc/MyDB) to the data source you created in WAS.

                  在您的代码中,您可以获得与您在示例中显示的方式类似的 DataSource;但是,用于查找的 JNDI 名称实际上应该是您定义的资源引用的名称 (res-ref-name),而不是物理数据源的 JNDI 名称.此外,您需要在 res-ref-name 前面加上应用程序命名上下文 (java:comp/env/).

                  In your code, you can obtain the DataSource similar to how you've shown it in your example; however, the JNDI name you'll use to look it up should actually be the resource reference's name you defined (res-ref-name), rather than the JNDI name of the physical data source. Also, you'll need to prefix the res-ref-name with the application naming context (java:comp/env/).

                  Context ctx = new InitialContext();
                  DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
                  

                  这篇关于如何使用给定的 JNDI 名称连接到 Websphere 数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <small id='8XZVe'></small><noframes id='8XZVe'>

                  <tfoot id='8XZVe'></tfoot>

                    <tbody id='8XZVe'></tbody>
                  <legend id='8XZVe'><style id='8XZVe'><dir id='8XZVe'><q id='8XZVe'></q></dir></style></legend>

                      • <i id='8XZVe'><tr id='8XZVe'><dt id='8XZVe'><q id='8XZVe'><span id='8XZVe'><b id='8XZVe'><form id='8XZVe'><ins id='8XZVe'></ins><ul id='8XZVe'></ul><sub id='8XZVe'></sub></form><legend id='8XZVe'></legend><bdo id='8XZVe'><pre id='8XZVe'><center id='8XZVe'></center></pre></bdo></b><th id='8XZVe'></th></span></q></dt></tr></i><div id='8XZVe'><tfoot id='8XZVe'></tfoot><dl id='8XZVe'><fieldset id='8XZVe'></fieldset></dl></div>
                          <bdo id='8XZVe'></bdo><ul id='8XZVe'></ul>