在Tomcat 配置Oracle 及DB2中创建Connection Pool

类别:Java 点击:0 评论:0 推荐:

1.寻找第三方的数据库java的驱动(如db2的驱动是db2java.zip文件,Oralce的驱动是classes12.zip),并且把它拷贝到Tomcat的库文件目录下。在这里是%TOMCAT_HOME%的common\lib子目录下。注意,一定要把文件变成.jar文件,再拷过来。否则可能出现认不出驱动的问题。

2.配置%TOMCAT_HOME%的conf下的server.xml文件。在Host分支下,删除不必要的Context(如最初安装可能装了Example等,注释掉相关描述)。加入自己的Context,以下是一个附有注释说明的例子:

<!-- 自己的Context,名称叫做GAP -->

 

 

        <Context path="/GAP" docBase="GAP" debug="0"

                 reloadable="true" crossContext="true">

                     <!-- 日志,在%TOMCAT_HOME%的logs下生成localhost_GAP_log.txt日志文件 -->

          <Logger className="org.apache.catalina.logger.FileLogger"

                     prefix="localhost_GAP_log." suffix=".txt"

                                  timestamp="true"/>

          <Ejb   name="ejb/EmplRecord" type="Entity"

                 home="com.wombat.empl.EmployeeRecordHome"

               remote="com.wombat.empl.EmployeeRecord"/>

<!-- 数据源名称,可以通过java:/comp/env/jdbc/oracle调用(连接池) -->

          <!-- If you wanted the examples app to be able to edit the

               user database, you would uncomment the following entry.

               Of course, you would want to enable security on the

               application as well, so this is not done by default!

               The database object could be accessed like this:

 

               Context initCtx = new InitialContext();

               Context envCtx = (Context) initCtx.lookup("java:comp/env");

               UserDatabase database =

                    (UserDatabase) envCtx.lookup("userDatabase");

          -->

 

 

          <Resource name="jdbc/oracle" auth="SERVLET"

                    type="javax.sql.DataSource"/>

                     <!-- 数据源参数表 -->

          <ResourceParams name="jdbc/oracle">

                                <!-- 数据源工厂,通过它得到DataSource -->

           <parameter>

                    <name>factory</name>

                      <value>

                        org.apache.commons.dbcp.BasicDataSourceFactory

                      </value>

             </parameter>

                                <!-- ORACLE 驱动的类名称 -->

            <parameter>

                <name>driverClassName</name>

                <value>

                         oracle.jdbc.driver.OracleDriver

                </value> 

           </parameter>

                  <!-- jdbc连接的url名称à

             <parameter>

                  <name>url</name>   

                <value>

                 jdbc:oracle:thin:@192.168.1.11:1521:ODAS

                </value>

            </parameter> 

            <parameter>

                <name>username</name>   

                <value>system</value>

            </parameter>

            <parameter>

                <name>password</name>   

                <value>odas</value> 

            </parameter>

            <parameter>   

                    <name>maxActive</name>

                   <value>50</value>

            </parameter> 

            <parameter>

                <name>maxIdle</name>   

                <value>10</value> 

            </parameter> 

            <parameter>

                 <name>maxWait</name>   

                <value>-1</value> 

            </parameter>

          </ResourceParams>

          <Resource name="jdbc/DB2" auth="SERVLET"

                    type="javax.sql.DataSource"/>

          <ResourceParams name="jdbc/DB2">

           <parameter>

                     <name>factory</name>

                       <value>

                        org.apache.commons.dbcp.BasicDataSourceFactory

                       </value>

            </parameter>

            <parameter>

                <name>driverClassName</name>

                <value>COM.ibm.db2.jdbc.app.DB2Driver</value> 

           </parameter>

            <parameter>

                <name>url</name>   

                <value>jdbc:db2:WST_DB</value>

            </parameter> 

             <parameter>

                <name>username</name>   

                <value>db2admin</value>

            </parameter>

            <parameter>

                <name>password</name>   

                <value>123456789</value> 

            </parameter>

            <parameter>   

                   <name>maxActive</name>

                   <value>50</value>

            </parameter> 

            <parameter>

                <name>maxIdle</name>   

                <value>10</value> 

            </parameter> 

            <parameter>

                <name>maxWait</name>   

                <value>-1</value> 

            </parameter>

          </ResourceParams>

         

        </Context>

3.在相应用到该连接池的Web-app(部署好的web应用,一般在%TOMCAT_HOME%的webapps目录下,也可以通过编辑server.xml来改变,下同)的WEB-INF下面修改web.xm l,在web-app分支内加入数据源

<resource-ref>

            <description>Oracle Connectiong Pool</description>

            <res-ref-name>jdbc/oracle</res-ref-name>

            <res-type>javax.sql.DataSource</res-type>

            <res-auth>Container</res-auth>

  </resource-ref>

  <resource-ref>

            <description>Oracle Connectiong Pool</description>

            <res-ref-name>jdbc/DB2</res-ref-name>

            <res-type>javax.sql.DataSource</res-type>

            <res-auth>Container</res-auth>

  </resource-ref>  

测试及调用方法

<%

  Connection conn=null;

  Statement stmt =null;

  ResultSet rs=null;

   

  try{

              Context initContext = new InitialContext();

              Context envContext=(Context)initContext.lookup("java:/comp/env");

              DataSource ds = (DataSource)envContext.lookup("jdbc/DB2");

              conn = ds.getConnection();

              if(conn!=null){

                       System.out.println("create connection  sucess!");

                       stmt = conn.createStatement();

                       if(stmt!=null){

                                 System.out.println("create stmt sucess!");

                                  rs=stmt.executeQuery("select * from WST_TB_CITY");

                                 while(rs.next()){

                                            out.println(rs.getString("CITYNAME")+"<br>");

                                 }

                       }else{

                                 System.out.println("create stmt fail!");

                      

                       }

               }else{

                       System.out.println("create stmt fail!");

              }

  }catch(Exception e){

             System.out.println(e);

  }

  finally{

             if(rs!=null)

                        rs.close();

             if(stmt!=null)

                        rs.close();

             if(conn!=null)

                        conn.close();

  }

%>

本文地址:http://com.8s8s.com/it/it11266.htm