---
title: "Tomcat JDNI Datasource"
author: "txazo"
date: "2012-09-03T08:28:09Z"
tags: []
categories: ["编程语言"]
slug: "tomcat-jdni-datasource"
draft: false
lastmod: "2012-09-03T08:28:08Z"
id: 1668226
url: "https://txazo.iteye.com/blog/1668226"
---

1. TOMCAT_HOME/conf/context.xml的<Context>标签下添加如下内容：

```xml

<Resource name="jdbc/txazo" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000"
    username="root" password="root"     
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/txazo"/>

```

2. WEB-INF/web.xml中添加如下内容：

```xml

<resource-ref>
    <res-ref-name>jdbc/txazo</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

```

3. Web应用中访问JNDI DataSource

```java

InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.
        lookup("java:comp/env/jdbc/txazo");
Connection connection = dataSource.getConnection();

```