惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
Cisco Talos Blog
Cisco Talos Blog
C
Cisco Blogs
A
Arctic Wolf
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
爱范儿
爱范儿
GbyAI
GbyAI
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
Cyberwarzone
Cyberwarzone
量子位
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
D
Docker
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
AI
AI
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
N
News | PayPal Newsroom
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
博客园 - 司徒正美
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric

博客园 - 老公鸡

sharepoint 2010? MVC2 学习之 UrlRouting Oracle 基础学习之索引 Oracle基础学习之函数 Oracle 基本操作之sql语句 Oracle基本操作 Oracle 数据库学习一 小黑日记三 结束了 mvc 3 学习 第一天 准备 小黑 成长日记第二天 2011年6月15日,同事捡了一只小猫回来了 NHibernate 学习 第十二天 存储过程的使用 NHibernate 学习 第十一天 对多对的关系处理 NHibernate 学习 第十天 一对多的外键关系处理 NHibernate 学习 第九天 外键关系的处理之一对一 NHibernate学习 第八天 组件的运用 NHibernate学习 第七天 其它的学习资料 NHibernate学习 第六天 NHibernate 中的查询 NHibernate 学习第五天 用户表的增,删,改,查
Jsp中使用数据库连接池.
老公鸡 · 2012-02-21 · via 博客园 - 老公鸡

1. 在tomcat服务器目录下面的conf中找到一个叫Context.xml的配置文件,在其中加入以下代码

<Resource name="jdbc/books" 
       auth
="Container"type="javax.sql.DataSource"  maxActive="100"  
       maxIdle
="30" maxWait="10000"   username="sa"   password="120010" 
      driverClassName
="com.microsoft.sqlserver.jdbc.SQLServerDriver"  
     url
="jdbc:sqlserver://localhost:1433;DatabaseName=news"/>

参数含义: JNDI (java naming and directory interface): Java 命名和目录接口

name

指定ResourceJNDI名称

auth

指定管理ResourceManagerContainer:由容器创建和管理|Application:由Web应用创建和管理)

type

指定Resource所属的Java

maxActive

指定连接池中处于活动状态的数据库连接的最大数目

maxIdle

指定连接池中处于空闲状态的数据库连接的最大数目

maxWait

指定连接池中的连接处于空闲的最长时间,超过这个时间会抛出异常,取值为-1,表示可以无限期等待

 

2. 第二步将 数据驱动.jar 放入tomcat目录下的lib或common\lib下面

3. 第三步,打开应用程序的 Web.xml文件,添加以下配置

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

节点数据来源于Context.xml里面设置的数据

4. 在java文件中先导入以下包

import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

将原来的数据库连接操作

Class.forName("oracle.jdbc.driver.OracleDriver");
if( conn == null || conn.isClosed() ) 
 conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",
 "system",
 "accp");


换成

Context c = new InitialContext();
DataSource ds = (DataSource)c.lookup("java:comp/env/jdbc/books");
conn = ds.getConnection();

记得要捕获 NamingException 与 SQLException 异常

使用连接池的好处是

数据库操作性能得到提升
通过连接池管理数据库的连接与释放、提高了系统资源的使用效率