---
title: "JNDI"
author: "klts"
date: "2012-03-15T11:54:48Z"
tags: []
categories: ["编程语言"]
slug: "jndi"
draft: false
lastmod: "2012-03-12T14:49:12Z"
id: 1450802
url: "https://klts.iteye.com/blog/1450802"
---

```
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

```

 config tomcat's resource factory

```
<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container"
            type="javax.sql.DataSource" username="dbusername" password="dbpassword"
            driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"
            maxActive="8" maxIdle="4"/>
  ...
</Context>

```