---
title: "JDBC 数据源知识 备忘录"
author: "manming"
date: "2010-12-23T15:20:40Z"
tags: []
categories: ["企业架构"]
slug: "jdbc"
draft: false
lastmod: "2010-12-23T15:20:40Z"
id: 850262
url: "https://manming.iteye.com/blog/850262"
---

**1.JDBC 创建CONNECTION 的两种方式**

  通过Class.forName()加载数据库驱动程序,然后再使用DriverManager.getConnection() 获取连接.

 

  通过javax.sql.DataSource提供了另一种建立数据库连接的方式.通过JNDI服务器查询,即调用Context.lookup,得到DataSource 对象,然后再调用DataSource的getConnection获取数据库连接.

 

 2.在Tommcat 中的Context中配置数据源

 

```
<Context reloadable="true">

    <Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource"

           maxActive="50" maxIdle="30" maxWait="10000"

           username="pl" password="123"

           driverClassName="com.mysql.jdbc.Driver"

           url="jdbc:mysql://localhost:3306/bookstore?autoReconnect=true" />

</Context>

```