1.在Tomcat的安装目录下的conf\Catalina\localhost 目录下建立一个与你打算给Web应用程序起的别名的文件:命名方式为:别名.xml
我的如下:gentoo.xml。注意这个名字要与 path里的值一样。
<Context
path="/gentoo"
docBase="D:\workplace\TomcatJDBC\WebRoot"
debug="0"
privileged="true">
<Resource name="jdbc/gentoo"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="root"
password="yang"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/web"/>
</Context>
2.使用:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/gentoo");
Connection conn = ds.getConnection();