JNDI+Tomcat配置+整合Spring

整合JNDI+Tomcat(适用于Tomcat6+7,Tomcat8不可以)+Spring。

主要参考:https://www.cnblogs.com/xdp-gacl/p/3951952.html

1、Tomcat配置

  1. 将对应的数据库jar放在Tomcat的lib目录下。
  2. 修改**\apache-tomcat\conf文件夹下server.xml配置文件。具体配置内容:
    <!-- Global JNDI resources
           Documentation at /docs/jndi-resources-howto.html
      -->
      <GlobalNamingResources>
        <!-- Editable user database that can also be used by
             UserDatabaseRealm to authenticate users
        -->
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml" />
    <!--
      |- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,
               为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
      |- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
      |- type:此名称所代表的类型,现在为javax.sql.DataSource
      |- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
      |- maxIdle:表示一个数据库在此服务器上维持的最小连接数
      |- maxWait:最大等待时间。10000毫秒
      |- username:数据库连接的用户名
      |- password:数据库连接的密码
      |- driverClassName:数据库连接的驱动程序
      |- url:数据库连接的地址
    -->
    <!--配置Oracle数据库的JNDI数据源-->
    <Resource 
            name="jdbc/oracle"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            username="lead_oams" 
            password="p"
            driverClassName="oracle.jdbc.driver.OracleDriver"
            url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>
    
    <!--配置MySQL数据库的JNDI数据源-->
    <Resource 
            name="jdbc/mysql"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            username="root" 
            password="root"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&amp;characterEncoding=utf-8"/>
    
    <!--配置SQLServer数据库的JNDI数据源-->
    <Resource 
            name="jdbc/sqlserver"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            username="sa" 
            password="p@ssw0rd"
            driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            url="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"/>
    
      </GlobalNamingResources>
  3. 修改**\apache-tomcat\conf文件夹下context.xml配置文件。具体配置内容:
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
    <!-- 第一种写法 -->
    <Resource 
        name="jdbc/oracle"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="ctpms" 
        password="ctpms"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@192.168.16.221:1521:orcl"/>
        <!-- 第二种写法(推荐)global是server.xml中的name,name是之后引用时对应的res-name -->
    <ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />

</Context>

2.javaWeb编写

1、配置web.xml文件(这个步骤可不写,我没有写,获取数据源没有问题)

<resource-ref>
    <description>Oracle DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name> 这个值是要和conf/context.xml文件中name属性值相同
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

2、获取数据源。在servlet编写测试代码。

try{
    DataSource dm = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
    Connection connMysql = dm.getConnection();
    String sql2 = "select * from SYS_USER"
    PreparedStatement pstmt2 = connMysql.prepareStatement(sql2);
    ResultSet set2 = pstmt2.executeQuery();
    while (set2.next()) {
        String type =  set2.getString(3);
	    System.out.println(type);
    }
} catch (NamingException e) {
   System.out.println(e.getMessage());
} catch (SQLException e) {
   e.printStackTrace();
} finally {
   try {
		connOracle.close();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}
response.getWriter().append("Served at: ").append(request.getContextPath());

3、修改Spring数据源

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
	<property name="jndiName">
	    <value>java:comp/env/jdbc/mysql</value>
	</property> 
</bean>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值