tomcat涓嬮€氳繃JNDI閰嶇疆鏁版嵁搴撹繛鎺ユ睜鎸夌収璁块棶鑼冨洿鍒嗙被鍏辨湁涓ょ锛屽垎鍒槸鍏ㄥ眬鏁版嵁搴撹繛鎺ユ睜鍜屽眬閮ㄦ暟鎹簱杩炴帴姹犮€傚墠鑰呮槸鍦ㄤ竴涓猼ocmat閮ㄧ讲涓嬬殑搴旂敤閮借兘浣跨敤璇ユ暟鎹簱杩炴帴姹狅紝鍚庤€呮槸鍙湁鐗瑰畾鐨勫簲鐢ㄦ墠鑳戒娇鐢ㄧ殑鏁版嵁搴撹繛鎺ユ睜銆倀omcat榛樿鐨凧DBC杩炴帴姹犲疄鐜版槸 apache涓嬬殑DBCP杩炴帴姹狅紝鎴戜滑涔熷彲浠ヤ娇鐢–3P0浣滀负tomcat鐨勬暟鎹簱杩炴帴姹狅紝浣嗘槸闇€瑕佽繘琛岄澶栫殑閰嶇疆锛屾湰绡囧崥瀹㈠彧浠嬬粛DBCP杩炴帴姹犵殑涓ょ閰嶇疆鏂瑰紡銆?
鍦ㄨ繘琛岄厤缃墠闇€瑕佸皢鏁版嵁搴撻┍鍔ㄦ嫹璐濆埌聽apache-tomcat-7.0.72\lib 鐩綍涓€傚鏋滀娇鐢–3P0锛岄渶瑕佸皢C3P0鐩稿叧鐨刯ar鎷疯礉鍒拌繖涓洰褰曚腑銆?
tomcat涓嬬殑鍏ㄥ眬鏁版嵁搴撹繛鎺ユ睜
绗竴姝ワ細鍦╟ontext.xml鏂囦欢娣诲姞Resource鑺傜偣
鍦╝pache-tomcat-7.0.72\conf\context.xml鏂囦欢涓坊鍔燫esource鑺傜偣锛屽唴瀹瑰涓嬶細
<!--
maxIdle 杩炴帴姹犱腑鏈€澶氬彲绌洪棽maxIdle涓繛鎺?
minIdle 杩炴帴姹犱腑鏈€灏戠┖闂瞞axIdle涓繛鎺?
initialSize 鍒濆鍖栬繛鎺ユ暟鐩?
maxWait 杩炴帴姹犱腑杩炴帴鐢ㄥ畬鏃?鏂扮殑璇锋眰绛夊緟鏃堕棿,姣
username 鏁版嵁搴撶敤鎴峰悕
password 鏁版嵁搴撳瘑鐮?-->
<Resource name="jndi/testConnDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/jiemian_db"
username="root"
password="123456"
maxActive="20"
maxIdle="10"
maxWait="10000"/>
/>
绗簩姝ワ細鍦ㄩ」鐩腑鐨剋eb.xml娣诲姞resource-ref鑺傜偣
<resource-ref>
<description>JNDI DataSource</description>
<res-ref-name>jndi/testConnDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
娉ㄦ剰锛歳es-ref-name鍏冪礌鐨勫唴瀹硅绗竴姝ョ殑resource鍏冪礌name灞炴€у€间竴鑷淬€?
tocmat涓嬬殑灞€閮ㄦ暟鎹簱杩炴帴姹犳柟寮忎竴
绗竴姝ワ細鍦?server.xml鏂囦欢涓坊鍔爎esource鑺傜偣
鍦╝pache-tomcat-7.0.72\conf\server.xml鏂囦欢鎵惧埌椤圭洰瀵瑰簲鐨凜ontext鍏冪礌涓嬫坊鍔犲涓嬪唴瀹癸細
<Context docBase="jdbc_exam" path="/jdbc_exam" reloadable="true" source="org.eclipse.jst.jee.server:jdbc_exam">
<!--
maxIdle 杩炴帴姹犱腑鏈€澶氬彲绌洪棽maxIdle涓繛鎺?
minIdle 杩炴帴姹犱腑鏈€灏戠┖闂瞞axIdle涓繛鎺?
initialSize 鍒濆鍖栬繛鎺ユ暟鐩?
maxWait 杩炴帴姹犱腑杩炴帴鐢ㄥ畬鏃?鏂扮殑璇锋眰绛夊緟鏃堕棿,姣
username 鏁版嵁搴撶敤鎴峰悕
password 鏁版嵁搴撳瘑鐮? -->
<Resource name="jndi/testConnDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/jiemian_db"
username="root"
password="123456"
maxActive="20"
maxIdle="10"
maxWait="10000"/>
</Context>
绗簩姝ワ細聽鍦ㄩ」鐩腑鐨剋eb.xml娣诲姞resource-ref鑺傜偣
鍚屼笂涓€鑺傜浜屾
tocmat涓嬬殑灞€閮ㄦ暟鎹簱杩炴帴姹犳柟寮忎簩(鎺ㄨ崘)
绗竴姝ワ細鍦ㄩ」鐩腑鏂板缓context.xml鏂囦欢,骞舵坊鍔燫esource鑺傜偣
鍦ㄩ」鐩腑鐨凪ETA-INF鐩綍鏂板缓context.xml鏂囦欢锛屾坊鍔犲涓嬪唴瀹癸細
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!--
maxIdle 杩炴帴姹犱腑鏈€澶氬彲绌洪棽maxIdle涓繛鎺?
minIdle 杩炴帴姹犱腑鏈€灏戠┖闂瞞axIdle涓繛鎺?
initialSize 鍒濆鍖栬繛鎺ユ暟鐩?
maxWait 杩炴帴姹犱腑杩炴帴鐢ㄥ畬鏃?鏂扮殑璇锋眰绛夊緟鏃堕棿,姣
username 鏁版嵁搴撶敤鎴峰悕
password 鏁版嵁搴撳瘑鐮? -->
<Resource name="jndi/testConnDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/jiemian_db"
username="root"
password="123456"
maxActive="20"
maxIdle="10"
maxWait="10000"/>
</Context>
绗簩姝ワ細聽鍦ㄩ」鐩腑鐨剋eb.xml娣诲姞resource-ref鑺傜偣
鍚屼笂涓€鑺傜浜屾銆?
娴嬭瘯鏁版嵁搴撹繛鎺ユ睜
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@page import="javax.naming.*"%>
<%@page import="javax.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<%
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jndi/testConnDB");
%>
<b>杩炴帴鏁版嵁搴撴槸鍚︽垚鍔?閫氳繃DataSource鏂规硶銆俒<%=ds.getConnection()!=null %>]</b>
</body>
</html>
鍙傝€冩枃绔狅細
http://www.cr173.com/html/19802_1.html
http://blog.csdn.net/lgm277531070/article/details/6711177