1.在自己的WEB应用的META-INF文件夹下新建context.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/appName" docBase="appName" debug="5" crossContext="true" reloadable="false" cachingAllowed="true" cacheMaxSize="20480" cacheTTL="10000"> <Resource name="jdbc/mysql" auth="Container" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" type="javax.sql.DataSource" maxActive="50" maxIdle="10" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true" />
</Context>
注意下改参数名字
2.在web.xml中加入如下代码,声明使用外部资源(resource-ref的位置应该蛮靠后的,我是扔在最后面了)
<resource-ref>
<description>DB Connection </description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.把JDBC驱动包甩到WEB的lib下面(我的是mysql-connector-java-5.1.15-bin.jar)
重启Server就OK 了,使用的时候这样获取Connection
Context context = new InitialContext();DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql");conn = ds.getConnection();
此文章由 http://www.ositren.com 收集整理 ,地址为:
http://www.ositren.com/htmls/68247.html