---
title: "tomcat 下配置jndi"
author: "vvovv"
date: "2012-05-22T18:37:08Z"
tags: []
categories: ["互联网"]
slug: "tomcat-jndi"
draft: false
lastmod: "2012-05-23T09:30:55Z"
id: 1538062
url: "https://vvovv.iteye.com/blog/1538062"
---

tomcat 下配置jndi(oracle数据源)

1.web.xml

增加配置信息：

```

<resource-ref>     <description>OracleDataSource</description>     <res-ref-name>oracleConn</res-ref-name>     <res-type>jndi名称</res-type>     <res-auth>Container</res-auth> </resource-ref> 
```

2.tomcat配置文件context.xml

增加配置信息：

```

<Resource name="jndi名称"  auth="Container"     type="javax.sql.DataSource"     username="用户名"      password="密码"     driverClassName="oracle.jdbc.driver.OracleDriver"      url="jdbc:oracle:thin:@数据库服务器域名或IP:1521:数据库名称"      maxActive="100"      maxIdle="300"      maxWait="60000"/>
```

3.1与spring集成使用，配置信息形如：

```

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">        <property name="jndiName"><value>java:comp/env/jndi名称</value></property>                </bean></beans>
```

3.2代码中使用，形如：

```
Context context = new InitialContext();DataSource dataSource = initCtx.lookup(jndi名称)if(dataSource != null){    Connection conn = dataSource.getConnection();}
```