1、tomcat中sever配置
<Resource auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="10"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" jmxEnabled="true"
logAbandoned="true" maxActive="100" maxWait="10000" minEvictableIdleTimeMillis="30000"
minIdle="10" name="jdbc/myweb" password="sa" removeAbandoned="true"
removeAbandonedTimeout="60" testOnBorrow="true" testOnReturn="false" testWhileIdle="true"
timeBetweenEvictionRunsMillis="30000" type="javax.sql.DataSource"
url="jdbc:sqlserver://192.168.0.1:4643;DatabaseName=test"
username="sa" validationInterval="30000" validationQuery="SELECT 1"/>
以上放在 此标签之内:<GlobalNamingResources> </GlobalNamingResources>
2、context中配置
<ResourceLink global="jdbc/SJHIS" name="jdbc/myweb" type="javax.sql.DataSource" />
以上放在此标签下:<WatchedResource>WEB-INF/web.xml</WatchedResource>
3、创建一个调用连接的类,类中的连接方法如下:
public Connection GetConnect(){
try {
DataSource ds = null;
Context initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/myweb");
return ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}