<Context crossContext="true" displayName="myjsp" docBase="D:/JsgTest" path="/myjsp" reloadable="true">
<Resource name="aaa" type="javax.sql.DataSource"/>
<ResourceParams name="aaa">
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs</value>
</parameter>
<parameter>
<name>password</name>
<value>kingapex</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
</ResourceParams>
</Context>
测试:
在D:\JsgTest目录下建文件:Test.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
Connection conn = null;
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("aaa");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from [titles]";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>